vendredi 11 septembre 2020

Use of ifelse to assign values to a new dataframe column in R

I have a time series dataframe and would like to create a new numeric column with values which are a function of an existing numeric column and which are assigned according to the day of the week column.

For example, I would require something like the following code:

Day <- c("Mo", "Mo", "Mo", "Tu", "Tu", "We", "We", "We", "We", "Th")
Val <- c(1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000)
df <- data.frame(cbind(Day,Val))
df$Adj <- ifelse(df$Day == "Mo" || df$Day == "Tu", 
                 as.numeric(levels(df$Val)) + 1, 
                 as.numeric(levels(df$Val)) + 2)

to return:

   Day  Val  Adj
1   Mo 1000 1001
2   Mo 1000 1001
3   Mo 1000 1001
4   Tu 1000 1001
5   Tu 1000 1001
6   We 1000 1002
7   We 1000 1002
8   We 1000 1002
9   We 1000 1002
10  Th 1000 1002

Unfortunately for me, my code instead returns Adj as a column of 1001s only.

   Day  Val  Adj
1   Mo 1000 1001
2   Mo 1000 1001
3   Mo 1000 1001
4   Tu 1000 1001
5   Tu 1000 1001
6   We 1000 1001
7   We 1000 1001
8   We 1000 1001
9   We 1000 1001
10  Th 1000 1001

I've tested the ifelse on one of the "We" rows and it does the trick ...

> ifelse(df$Day[6] == "Mo" || df$Day[6] == "Tu", 
+        as.numeric(levels(df$Val[6])) + 1, 
+        as.numeric(levels(df$Val[6])) + 2)
[1] 1002

... but I can't seem to get it to work on an entire column, which I had understood is one of the advantages of the ifelse function over a looped if-else statement.

I'm basing my approach off of the most similar question I could find (Create new column in dataframe using if {} else {} in R) but have had no joy. What am I missing here?

Aucun commentaire:

Enregistrer un commentaire