Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/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
R 从列中提取列表数据_R - Fatal编程技术网

R 从列中提取列表数据

R 从列中提取列表数据,r,R,我有一个看起来非常类似的数据框(基本上是地图上城市及其坐标的表格。请注意,坐标是X,Y值的列表) foo <- data.frame( city = c("chicago", "new york"), coordinate = I(list(list(10, 15), list(20, 25))), myabbr = c("chi", "ny") ) bar <- subset(foo, select=c("city", "coordinate")) foo您可以

我有一个看起来非常类似的数据框(基本上是地图上城市及其坐标的表格。请注意,坐标是X,Y值的列表)

foo <- data.frame(
  city = c("chicago", "new york"), 
  coordinate = I(list(list(10, 15), list(20, 25))), 
  myabbr = c("chi", "ny")
)

bar <- subset(foo, select=c("city", "coordinate"))

foo您可以使用
list[[index]]
访问列表元素。在您的情况下,您可以通过以下方式提取它:

foo <- data.frame(city=c("chicago", "new york"), coordinate=I(list(list(10, 15), list(20,25))), myabbr=c("chi", "ny"))
foo$coordinate_x = foo$coordinate[[1]]
foo$coordinate_y = foo$coordinate[[2]]
foo

foo您需要的是从列表列“坐标”中提取X和Y元素。列表提取就像R中的
list[[index]]
一样完成

i、 e


foo这里的想法是
unlist
每行坐标和
cbind
与城市的坐标,即

cbind(city = as.character(bar$city), 
      setNames(data.frame(apply(bar, 1, function(i)unlist(i$coordinate))), 
               c('coordinate1', 'coordinate2')))
这就给了,


我们可以取消列列表并绑定回原始数据帧,请尝试:

cbind(foo, do.call(rbind, lapply(foo$coordinate, unlist)))
#       city coordinate myabbr  1  2
# 1  chicago     10, 15    chi 10 15
# 2 new york     20, 25     ny 20 25

你也可以尝尝小提花

library(tidyverse)

foo %>% 
   mutate(coordinate=map(coordinate,~unlist(.) %>% 
                      paste(., collapse=","))) %>% 
   separate(coordinate, into = c("x", "y"), sep=",")
# A tibble: 2 x 4
  city     x     y     myabbr
  <fct>    <chr> <chr> <fct> 
1 chicago  10    15    chi   
2 new york 20    25    ny  

您还可以使用
splitstackshape
中的
listCol\u w

library(splitstackshape)
listCol_w(foo, "coordinate")
#       city myabbr coordinate_fl_1 coordinate_fl_2
#1:  chicago    chi              10              15
#2: new york     ny              20              25
bar%>%
组别(城市)%>%
变异(坐标=列表(取消列表(坐标)),
n=列表(粘贴0(“坐标”,1:长度(坐标)))%>%
最新%>%
排列(n,坐标)
#一个tibble:2x3
#组别:城市[2]
城市协调人1协调人2
芝加哥1号10点15分。
纽约20.25。

您可以尝试类似于
cbind(as.character(bar$city),apply(bar,1,function(i)unlist(i$coordinate))
我将给出一个shotAbout
cbind(foo,do.call(rbind,lappy(foo$coordinate,unlist)))
?这在@zx8754上有效。他们在技术上都有效,但其中一个有正确的DataTypesHanks!这实际上是我最终使用的!因为你依赖于一个高度特定的包,我可以告诉你,大多数R开发人员只会使用内部
[[
函数。每个人都会理解它,而
listcolu\w
需要一条注释才能理解或在帮助中查找。
cbind(foo, do.call(rbind, lapply(foo$coordinate, unlist)))
#       city coordinate myabbr  1  2
# 1  chicago     10, 15    chi 10 15
# 2 new york     20, 25     ny 20 25
library(tidyverse)

foo %>% 
   mutate(coordinate=map(coordinate,~unlist(.) %>% 
                      paste(., collapse=","))) %>% 
   separate(coordinate, into = c("x", "y"), sep=",")
# A tibble: 2 x 4
  city     x     y     myabbr
  <fct>    <chr> <chr> <fct> 
1 chicago  10    15    chi   
2 new york 20    25    ny  
.Last.value %>% 
select(-myabbr)
library(splitstackshape)
listCol_w(foo, "coordinate")
#       city myabbr coordinate_fl_1 coordinate_fl_2
#1:  chicago    chi              10              15
#2: new york     ny              20              25
bar%>%
   group_by(city)%>%
   mutate(coordinate=list(unlist(coordinate)),
          n=list(paste0("coordinate",1:lengths(coordinate))))%>%
   unnest%>%
   spread(n,coordinate)

# A tibble: 2 x 3
# Groups:   city [2]
  city     coordinate1 coordinate2
  <fct>          <dbl>       <dbl>
1 chicago          10.         15.
2 new york         20.         25.