Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将向量第I个位置的值分配给data.frames列表第I个元素中的单元格?_R - Fatal编程技术网

如何将向量第I个位置的值分配给data.frames列表第I个元素中的单元格?

如何将向量第I个位置的值分配给data.frames列表第I个元素中的单元格?,r,R,我想将向量的第I个值指定给data.frames列表中第I个元素的位置[1,1] 例如,我想修改“list1”,如下所示: # list of data.frames list1 <- list(data.frame(v1=c("a","x"),v2=c("x","x")), data.frame(v1=c("b","x"),v2=c("x",&quo

我想将向量的第I个值指定给data.frames列表中第I个元素的位置
[1,1]

例如,我想修改“list1”,如下所示:

# list of data.frames
list1 <- list(data.frame(v1=c("a","x"),v2=c("x","x")), data.frame(v1=c("b","x"),v2=c("x","x")))
print(list1)

    [[1]]
      v1 v2
    1  a  x
    2  x  x
    
    [[2]]
      v1 v2
    1  b  x
    2  x  x

# vector of new values
new_elements <- c("c", "d")

# desired output
    [[1]]
      v1 v2
    1  c  x
    2  x  x
    
    [[2]]
      v1 v2
    1  d  x
    2  x  x
#数据帧列表

列表1您可以使用基本R中的
Map
。这会将任意两个参数函数作为其第一个参数,然后将两个长度相同的对象作为其第二个和第三个参数。然后将函数应用于这些对象的并行元素,并将结果作为列表返回:

Map(函数(df,元素){df[1,1][[1]]
#>v1 v2
#>1立方厘米
#>2 x x
#> 
#> [[2]]
#>v1 v2
#>1dx
#>2 x x

A
purrr
解决方案:

library(purrr)
map2(list1, new_elements, function(x, y) {
  x[1, 1] = y
  x
})
# [[1]]
#   v1 v2
# 1  c  x
# 2  x  x

# [[2]]
#   v1 v2
# 1  d  x
# 2  x  x
library(purrr)
map2(list1, new_elements, function(x, y) {
  x[1, 1] = y
  x
})
# [[1]]
#   v1 v2
# 1  c  x
# 2  x  x

# [[2]]
#   v1 v2
# 1  d  x
# 2  x  x