[英]Create new column in each dataframe of list and fill with string from character vector based on position (R)
I have a list of data frames and a character vector with strings.我有一个数据框列表和一个带字符串的字符向量。 The number of dataframes and the number of strings in the
chr
are the same.数据帧的数量和
chr
中的字符串数量相同。
I'd like to populate a specific column in each dataframe in the list with the string at the corresponding position in the character vector我想用字符向量中相应位置的字符串填充列表中每个数据框中的特定列
dfs<-list(mtcars[,1:4], iris[,1:4])
dfs <- lapply(dfs, function(x) transform(x, mycol=""))
z <- c("red", "blue")
As the final output I'd like作为我想要的最终输出
dfs[[1]]$mycol
to be populated with red
and dfs[[1]]$mycol
填充red
和
dfs[[2]]$mycol
to be populated with blue
dfs[[2]]$mycol
用blue
填充
Conceptually, I think I need to do something like this:从概念上讲,我认为我需要做这样的事情:
dfs <- lapply(dfs, function(n) dfs[[n]]$mycol <- z[n])
, but I get the error dfs <- lapply(dfs, function(n) dfs[[n]]$mycol <- z[n])
,但我得到错误
Error in z[n] : invalid subscript type 'list'
z[n] 中的错误:无效的下标类型“列表”
The real data is a list of 97 elements真正的数据是一个包含 97 个元素的列表
You can also try creating directly mycol
with mapply()
:您也可以尝试使用
mapply()
直接创建mycol
:
#Data
dfs<-list(mtcars[,1:4], iris[,1:4])
z <- c("red", "blue")
#Code
L <- mapply(function(x,y) {x$mycol<-y;return(x)},x=dfs,y=z,SIMPLIFY = F)
This is what you're after这就是你所追求的
lapply(1:n, function(x) transform(dfs[x], mycol = z[x]))
When you want to perform an apply
by passing an index on several objects, simply apply
on 1:n
then pass this as an argument to the different objects within the anonymous function.当您想通过在多个对象上传递索引来执行
apply
,只需在1:n
上apply
,然后将其作为参数传递给匿名函数中的不同对象。
EDIT transform
only works with all objects contained in the same environment.编辑
transform
仅适用于包含在同一环境中的所有对象。 So it throws the error object not found
with the previous code because dfs
exists the .GlobalEnv
while x
exists only in the function environments.所以它抛出了之前代码中
object not found
的错误object not found
,因为dfs
存在.GlobalEnv
而x
只存在于函数环境中。
The below code works下面的代码有效
lapply(c(1:n), function(x) {
toreplace <<- z[x] # forcing to parent envir using <<-
base::transform(dfs[x], mycol = toreplace)
})
We could use Map
with transform
in base R
我们可以在
base R
使用Map
和transform
Map(transform, dfs, mycol = z)
Or map2
from purrr
或者来自
purrr
map2
library(purrr)
library(dplyr)
map2(dfs, z, ~ .x %>%
mutate(mycol = .y))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.