My dataframe looks like this:
var1 var2 var3
1 2 5 "other"
2 25 3 "sample"
3 4 5 "baseline_other"
4 60 5 "baseline_sample"
5 40 5 "other"
6 60 5 "other"
7 25 3 "sample"
8 6 8 "other"
9 60 7 "other"
10 4 3 "other"
I want to add a new column in which I do a calculation that uses the value of df$var1
of row df$var3==baseline_other
in case the df$var3 == "other"
.
And if df$var3 == "sample"
it should use the value of df$var1
of row df$var3==baseline_sample
. (I hope i'm making myself clear). So i would like to give x_other
the value of df$var1
in row baseline_other
. (And the same for x_sample
using baseline_sample
).
I tried x_other <- df[df$var3=="baseline_other",df$var1]
but i get an error saying undefined columns selected
.
When I could make this work I would further like to add a column with a calculation (as I mentioned above) that would then look like this:
df$new <- ifelse(df$var3=="other", (abs(df$var1-x_other)/x_other), NA)
and repeat this for "sample" so:
df$new <- ifelse(df$var3=="sample", (abs(df$var1-x_sample)/x_sample), NA)
I hope I made myself clear enough. But my problem is to actually select a certain value in my dataframe by row name and column and then use that value in a calculation.
My new df should look like this:
var1 var2 var3 var4
1 2 5 "other" 0.5
2 25 3 "sample" 0.6
3 4 5 "baseline_other" NA
4 60 5 "baseline_sample" NA
5 40 5 "other" 9
6 60 5 "other" 14
7 25 3 "sample" 0.6
8 6 8 "other" 0.5
9 60 7 "other" 14
10 4 3 "sample" 0.9