如何添加向量列表的数据。框架对象作为新插槽并行? [英] How to add list of vector to list of data.frame objects as new slot by parallel?
本文介绍了如何添加向量列表的数据。框架对象作为新插槽并行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
玩具资料
df.list<列表(
d1 < - data.frame(
V1 = seq(1,by = 2,len = 6)),V2 = seq(6,by = 2,len = 6),
V3 =字母[seq(1:6)],V4 =样本(1:10,6,替换= FALSE)),
d2 < - data.frame(
V1 = seq 2 = 3,len = 8),V2 = seq(6,by = 3,len = 8),
V3 =字母[seq(1:8)],V4 = 8,replace = FALSE)),
d3< - data.frame(
V1 = seq(4,by = 3,len = 5),V2 = seq(9,by = 3,len = 5),
V3 =字母[seq(1:5)],V4 = sample(1:8,5,replace = FALSE))
)
我想通过并行添加它们的向量:
vec.list< - list(sc1 <-C(1,3,5,6,7,8),
sc2 < - c(2,4,5,7,9, 10,14,16),
sc3< - c(6,10,11,13,15))
解决方案
尝试这样:
library(dplyr)
库(purrr)
df.list.new< - map2(.x = d f.list,
.y = vec.list,
.f = function(x,y)bind_cols(x,data.frame('sc'= y))
I have list of vector with different dimension and list of different data.frame objects respectively. I want to add vec.list to df.list by parallel as new attributes. How can I make this happen in fastest way? Thanks in advance
toy data
df.list <- list(
d1 <- data.frame(
V1=seq(1, by=2, len=6), V2=seq(6, by=2, len=6),
V3=letters[seq(1:6)], V4=sample(1:10, 6, replace = FALSE)),
d2 <- data.frame(
V1=seq(2, by=3, len=8), V2=seq(6, by=3, len=8),
V3=letters[seq(1:8)], V4=sample(1:10, 8, replace = FALSE)),
d3 <- data.frame(
V1=seq(4, by=3, len=5), V2=seq(9, by=3, len=5),
V3=letters[seq(1:5)], V4=sample(1:8, 5, replace = FALSE))
)
the vector I want to add them by parallel:
vec.list <- list(sc1 <- c(1,3,5,6,7,8),
sc2 <- c(2,4,5,7,9,10,14,16),
sc3 <- c(6,10,11,13,15))
解决方案
Try this:
library(dplyr)
library(purrr)
df.list.new <- map2(.x = df.list ,
.y = vec.list ,
.f = function(x, y) bind_cols(x, data.frame('sc' = y)))
这篇关于如何添加向量列表的数据。框架对象作为新插槽并行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文