0
df1
vec1 vec2 vec3
0     10   5
10    20   13
20    30   28

I would like to create a new column as shown below that contains the median value of a list of vectors in the data frame at its row index.

df1
vec1  vec2 vec3 median
0     10   5    5
10    12   13   12
20    30   28   28
pnuts
  • 58,317
  • 11
  • 87
  • 139
James H
  • 159
  • 1
  • 8
  • 1
    `df1$median <- apply(df,1,median)` – scoa Sep 08 '15 at 21:14
  • @scoa what if I only want to apply this to vectors 2 and 3? – James H Sep 08 '15 at 21:23
  • `df1$median <- apply(df[,2:3],1,median)`. But the median of two values does not make a lot of sense... – scoa Sep 08 '15 at 21:25
  • @scoa thanks. I know I will be applying it to several columns that aren't always the first or last in the data frame. I didn't want to stray too much with the question though. This answers everything. – James H Sep 08 '15 at 21:29

0 Answers0