简体   繁体   中英

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?)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM