I have the following dataframe (df
)
start end 1 14379 32094 2 151884 174367 3 438422 449382 4 618123 621256 5 698271 714321 6 973394 975857 7 980508 982372 8 994539 994661 9 1055151 1058824 . . . . . . . . .
And a long vector with numeric values (vec
).
I would like to add to each row another column, with the mean of the values in the corresponding places in vec
. for example, the first row will have mean(vec[14379:32094])
. I have tried playing with transform
but wasn't able to accomplish this simple task.
add_column() function from tidyverse can also be used to add an empty column to DataFrame in R. In order to use this function first, you need to install R package by using install. packages("tidyverse") and load it using the library("tidyverse") .
Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector.
That is a pretty standard use case for apply()
:
R> vec <- 1:10 R> DF <- data.frame(start=c(1,3,5,7), end=c(2,6,7,9)) R> DF$newcol <- apply(DF,1,function(row) mean(vec[ row[1] : row[2] ] )) R> DF start end newcol 1 1 2 1.5 2 3 6 4.5 3 5 7 6.0 4 7 9 8.0 R>
You can also use plyr
if you prefer but here is no real need to go beyond functions from base R.
Even if that's a 7 years old question, people new to R should consider using the data.table, package.
A data.table is a data.frame so all you can do for/to a data.frame you can also do. But many think are ORDERS of magnitude faster with data.table.
vec <- 1:10 library(data.table) DT <- data.table(start=c(1,3,5,7), end=c(2,6,7,9)) DT[,new:=apply(DT,1,function(row) mean(vec[ row[1] : row[2] ] ))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With