0

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
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • why the 'var1' is changed in the input and expected? – akrun Feb 07 '18 at 11:15
  • 1
    In `df[df$var3=="baseline_other",df$var1]`, you are doing an inappropriate column subscript. That should simply be `df[df$var3=="baseline_other", "var1"]` – RolandASc Feb 07 '18 at 11:17
  • @akrun sorry my mistake, edited it! – R_beginner_22 Feb 07 '18 at 11:20
  • I thought a [nested ifelse](https://stackoverflow.com/questions/18012222/nested-ifelse-statement) statement would do the trick, like this: `df$calc <- ifelse(df$var3 == "other", abs(((df$var1[df$var3 == "other"])-(df$var1[df$var3 == "baseline_other"])))/(df$var1[df$var3 == "baseline_other"]), ifelse(df$var3 == "sample", abs(((df$var1[df$var3 == "sample"])-(df$var1[df$var3 == "baseline_sample"])))/(df$var1[df$var3 == "baseline_sample"]), NA))`. But the order of "other" values is off, while outside of ifelse it's fine. – 4rj4n Feb 07 '18 at 15:30
  • Perhaps that order change mentioned in my first comment has to do with [this](https://stackoverflow.com/questions/44699799/r-ifelse-unexpected-change-in-vector-order), but I don't understand how that applies here and how to fix it. – 4rj4n Feb 07 '18 at 15:32

0 Answers0