Search code examples
rassigndatasheet

Dependant variable column in R


I'm trying to add a newcolumn of information to a data table in R, I have a column

dataSheet$day

with numerical values 1 to 3 with 1 being Thursday, 2 being Friday and 3 being Saturday I want to assign the value "week" or "weekend" to a new variable

dataSheet$t_week

With dataSheet$t_week being "week" when dataSheet$day is 1, and dataSheet$t_week being "weekend" when dataSheet$day is 2 or 3. This is the code I tried:

if(dataSheet$day == 2) {dataSheet$t_week = "Week"} else {dataSheet$t_week = "Weekend"}

when I try this I get a column with every element in it being Weekend, regardless of the value in dataSheet$day. ( I think this might be the case because the first element in dataSheet$day is 2 and it iterates over it?)


Solution

  • without having data to work with, a good place to start is ifelse():

    dataSheet$t_week<- ifelse(dataSheet$day == 1, "Week", "Weekend")
    

    Is this working for you? check it out.