Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
用R中的列表填充数据帧_R_List - Fatal编程技术网

用R中的列表填充数据帧

用R中的列表填充数据帧,r,list,R,List,我想用列表填充数据框的一列。根据我找到的示例代码: d这里发生了几件事。当使用单括号[]或双括号[[]]进行索引时,R会产生不同的行为。简而言之,当使用单括号索引数据帧时,R需要(或返回)列表对象。使用双括号时,将返回基础向量 请注意,下面的第一个示例(使用单括号)保留了数据框列的结构和命名,而双括号示例将列的基本内容作为向量返回 > str(mtcars['mpg']) 'data.frame': 32 obs. of 1 variable: $ mpg: num 21 21

我想用列表填充数据框的一列。根据我找到的示例代码:


d这里发生了几件事。当使用单括号
[]
或双括号
[[]]
进行索引时,R会产生不同的行为。简而言之,当使用单括号索引数据帧时,R需要(或返回)列表对象。使用双括号时,将返回基础向量

请注意,下面的第一个示例(使用单括号)保留了数据框列的结构和命名,而双括号示例将列的基本内容作为向量返回

> str(mtcars['mpg'])
'data.frame':   32 obs. of  1 variable:
 $ mpg: num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...

> str(mtcars[['mpg']])
 num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
要回答您的问题,为什么对
list()
的多余调用会有帮助,
str
可以帮助您了解这一问题:

没有额外的
list()
的原始代码是长度为2的列表:

> str(list(list("Mary", "James"), list("Greta", "Sally")))

List of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"
这会失败,因为
d['children']
希望与长度为1的对象匹配。但是,添加额外的
list()
会创建一个长度为1的“外部”列表,因此分配成功

str(list(list(list("Mary", "James"), list("Greta", "Sally"))))

List of 1
 $ :List of 2
  ..$ :List of 2
  .. ..$ : chr "Mary"
  .. ..$ : chr "James"
  ..$ :List of 2
  .. ..$ : chr "Greta"
  .. ..$ : chr "Sally"
最后,如果您使用双括号索引,您的原始代码(没有额外的
list()
)将可以工作:

d[["children"]] <-  list(list("Mary", "James"), list("Greta", "Sally"))

d[[“children”]@jdobres answer让我玩了下面的例子,这有助于我理解(某种程度上)发生了什么

> d <- data.frame(id=1:2, name=c("Jon", "Mark"))
> d
  id name
1  1  Jon
2  2 Mark
> add <- list(list("Mary", "James"), list("Greta", "Sally"))
> d$children <- add
> d
  id name     children
1  1  Jon  Mary, James
2  2 Mark Greta, Sally
> str(d$children)
List of 2                                  # d$children is a list of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"
> str(add)
List of 2                                  # add is a list of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"
>d
身份证名称
乔恩
2马克
>加d$children d
孩子的id名称
乔恩·玛丽,詹姆斯
马克·格雷塔,萨利
>str(d$儿童)
2#d$儿童列表是一个2人列表
$:2人名单
..$:chr“玛丽”
..$:chr“James”
$:2人名单
..$:chr“Greta”
..$:chr“Sally”
>str(添加)
列表2#添加是一个列表2
$:2人名单
..$:chr“玛丽”
..$:chr“James”
$:2人名单
..$:chr“Greta”
..$:chr“Sally”
这是因为
d$children d的lhs和rhs
身份证名称
乔恩
2马克
>添加d[“children”]str(d[“children”])
“data.frame”:2个obs。1个变量的组合:#d[“children”]是1个变量,带有2个OB。
$children:2人名单
..$:chr“玛丽”
..$:chr“James”
>str(添加)
列表2#添加是一个列表2
$:2人名单
..$:chr“玛丽”
..$:chr“James”
$:2人名单
..$:chr“Greta”
..$:chr“Sally”
这不起作用,因为
d$children d的lhs添加了d[“children”]d
孩子的id名称
乔恩·玛丽,詹姆斯
马克·格雷塔,萨利
>str(d[“儿童”])
“data.frame”:2个obs。1个变量的组合:#d[“children”]是1个变量,带有2个OB。
$children:2人名单
..$:2人名单
.. ..$ : “玛丽”
.. ..$ : chr“詹姆斯”
..$:2人名单
.. ..$ : “格丽塔”
.. ..$ : “莎莉”
>str(添加)
1个列表#添加是1个列表和2个列表
$:2人名单
..$:2人名单
.. ..$ : “玛丽”
.. ..$ : chr“詹姆斯”
..$:2人名单
.. ..$ : “格丽塔”
.. ..$ : “莎莉”
这里的命名法有点不合逻辑,但如果您接受列表必须在列表中才能算作列表,那么上面的方法是有效的,因为
d$children d的lhs
身份证名称
乔恩
2马克
>添加d[[“子项”]]d
孩子的id名称
乔恩·玛丽,詹姆斯
马克·格雷塔,萨利
>str(d[[“儿童”]]
2#d[[“儿童”]]的列表是一个2人的列表
$:2人名单
..$:chr“玛丽”
..$:chr“James”
$:2人名单
..$:chr“Greta”
..$:chr“Sally”
>str(添加)
列表2#添加是一个列表2
$:2人名单
..$:chr“玛丽”
..$:chr“James”
$:2人名单
..$:chr“Greta”
..$:chr“Sally”

像第一个例子一样,这很有效,因为
d$children的lhs和rhs非常感谢,这真的很有帮助!我已经接受了你的答案,但我将添加一个有助于我理解的附加示例的答案。
> str(list(list("Mary", "James"), list("Greta", "Sally")))

List of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"
str(list(list(list("Mary", "James"), list("Greta", "Sally"))))

List of 1
 $ :List of 2
  ..$ :List of 2
  .. ..$ : chr "Mary"
  .. ..$ : chr "James"
  ..$ :List of 2
  .. ..$ : chr "Greta"
  .. ..$ : chr "Sally"
d[["children"]] <-  list(list("Mary", "James"), list("Greta", "Sally"))
> d <- data.frame(id=1:2, name=c("Jon", "Mark"))
> d
  id name
1  1  Jon
2  2 Mark
> add <- list(list("Mary", "James"), list("Greta", "Sally"))
> d$children <- add
> d
  id name     children
1  1  Jon  Mary, James
2  2 Mark Greta, Sally
> str(d$children)
List of 2                                  # d$children is a list of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"
> str(add)
List of 2                                  # add is a list of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"
> d <- data.frame(id=1:2, name=c("Jon", "Mark"))
> d
  id name
1  1  Jon
2  2 Mark
> add <- list(list("Mary", "James"), list("Greta", "Sally"))
> d["children"] <- add
Warning message:
In `[<-.data.frame`(`*tmp*`, "children", value = list(list("Mary",  :
  provided 2 variables to replace 1 variables
> d
  id name children
1  1  Jon     Mary
2  2 Mark    James
> str(d["children"])
'data.frame':   2 obs. of  1 variable:     # d["children"] is 1 var. with 2 obs.
 $ children:List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
> str(add)
List of 2                                  # add is a list of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"
> d <- data.frame(id=1:2, name=c("Jon", "Mark"))
> add <- list(list(list("Mary", "James"), list("Greta", "Sally")))
> d["children"] <- add
> d
  id name     children
1  1  Jon  Mary, James
2  2 Mark Greta, Sally
> str(d["children"])
'data.frame':   2 obs. of  1 variable:     # d["children"] is 1 var. with 2 obs.
 $ children:List of 2
  ..$ :List of 2
  .. ..$ : chr "Mary"
  .. ..$ : chr "James"
  ..$ :List of 2
  .. ..$ : chr "Greta"
  .. ..$ : chr "Sally"
> str(add)
List of 1                                  # add is 1 list with 2 lists
 $ :List of 2
  ..$ :List of 2
  .. ..$ : chr "Mary"
  .. ..$ : chr "James"
  ..$ :List of 2
  .. ..$ : chr "Greta"
  .. ..$ : chr "Sally"
> d <- data.frame(id=1:2, name=c("Jon", "Mark"))
> d
  id name
1  1  Jon
2  2 Mark
> add <- list(list("Mary", "James"), list("Greta", "Sally"))
> d[["children"]] <- add
> d
  id name     children
1  1  Jon  Mary, James
2  2 Mark Greta, Sally
> str(d[["children"]])
List of 2                                  # d[["children"]] is a list of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"
> str(add)
List of 2                                  # add is a list of 2
 $ :List of 2
  ..$ : chr "Mary"
  ..$ : chr "James"
 $ :List of 2
  ..$ : chr "Greta"
  ..$ : chr "Sally"