I need to create a new column in my data frame, with value value chosen from another column, with a different name per row.
E.g., I have something like this data frame, df, describing what happened (events like "a", "h" and so on) to some persons (id 10,12,13..23) on seven days in a week:
id day mon tue wed thu fri sat sun
10 wed a y b j j b a
12 wed b e h y b h b
13 tue h y j b h j h
14 thu j u b h j b j
16 thu y i h j y h y
19 fri e y j y a j e
20 sun y e y a b y y
21 mon u y a b h a u
23 mon i u b h j b i
I need a new column "val", showing the value on the day mentioned in the "day" variable.
Hence, like this:
id day val mon tue wed thu fri sat sun
10 wed b a y b j j b a
12 wed h b e h y b h b
13 tue y h y j b h j h
14 thu h j u b h j b j
16 thu j y i h j y h y
19 fri a e y j y a j e
20 sun y y e y a b y y
21 mon u u y a b h a u
23 mon i i u b h j b i
I tried making a function I could apply on a column to produce a new column
lookupfunction <- function(x) {
rownumberofx <- which(x=x)
dayvalue <- df[rownumberofx,"day"]
dayvalue
rownumberofx <- NULL
}
df$val <- lookupfunction(df$day)
I hope to learn a code to produce the column "val"