subsetting across multiple data frames in R based on character vector values -
i have 6 data frames 2 columns each "id" , "value" , differing number of rows. labeled p1, p2....p6 , this
id value_p1 jane c 9.713457e-01 claire k 1.260160e-01 brett f 4.933005e-0 jen s 0.56
i have character vector includes intersect of names across 6 data sets called id_ intersect:
c("jane c", "claire k","brett f")
i want create new data frame has subset of sections in each data frame contain id_intersect , keeps values intersecting ids in each data frame.
id value_p1 value_p2.............value_p6 jane c 9.713457e-01 0.87 .098 claire k 1.260160e-01 0.89 .005 brett f 4.933005e-0 0.002 .035
i'm stuck on , i'm pretty new r appreciated. thanks.
you can use reduce
here merge
,
ll <- list(p1,p2,...,p6) reduce(merge,ll) ##
here re producible example. first create data frames within list
ll <- lapply(1:6, function(x) { id <- c(letters[1:3],letters[3+x]) dat <- data.frame(id=id, value=rnorm(4)) names(dat) <- c('id',paste('value',x,sep='_')) dat })
then
reduce(merge,ll) id value_1 value_2 value_3 value_4 value_5 value_6 1 brett f -0.09242725 -0.03908275 0.5366957 0.4926749 0.830829230 1.4868564 2 claire k -0.27487913 -0.13733120 -0.2708968 0.1720550 -0.003194644 -0.6328486 3 jane c -1.56306487 -0.75655434 0.1806619 0.2482159 -1.075606294 1.9711559
Comments
Post a Comment