【发布时间】:2019-03-23 20:45:09
【问题描述】:
我有两组列表,格式如下:
list(list(structure(list(X = c(3L, 4L, 5L, 7L, 2L, 8L, 9L, 6L,
10L, 1L), Y = structure(c(2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L,
1L), .Label = c("no", "yes"), class = "factor")), .Names = c("X",
"Y"), row.names = c(NA, -10L), class = "data.frame"), structure(list(
X = c(3L, 4L, 5L, 7L, 2L, 8L, 9L, 6L, 10L, 1L), Y = structure(c(2L,
2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L), .Label = c("no", "yes"
), class = "factor")), .Names = c("X", "Y"), row.names = c(NA,
-10L), class = "data.frame")))
和
list(list(structure(list(X = c(10L, 3L, 4L, 9L, 8L, 2L, 5L, 7L,
1L, 6L), Y = structure(c(2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L,
1L), .Label = c("no", "yes"), class = "factor")), .Names = c("X",
"Y"), row.names = c(NA, -10L), class = "data.frame"), structure(list(
X = c(5L, 7L, 4L, 3L, 10L, 2L, 9L, 1L, 8L, 6L), Y = structure(c(2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("no", "yes"
), class = "factor")), .Names = c("X", "Y"), row.names = c(NA,
-10L), class = "data.frame")))
My objective is to replace a[[1]][[i]]$x <- b[[1]][[i]]$x
当两个数据框在列表之外时,这相当简单:
df1$x<-df2$x
但是我写的代码不起作用
replacex<-function(onelist, anotherlist){
newlist<-list() #for storage
onelist$x<-anotherlist$x
newlist<-onelist
}
Dfs_new_X<-lapply(a,lapply,replacex,anotherlist=b)
它不会给出错误,但它会删除该列。
任何帮助将不胜感激。
【问题讨论】:
-
如果有人对我的代码为什么不工作有发言权,我们将不胜感激,所以要学习,以后不要犯同样的错误。
标签: r list dataframe tidyverse purrr