Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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_Tidyr_Tibble - Fatal编程技术网

R 将列表列直接卸载到多个列中

R 将列表列直接卸载到多个列中,r,tidyr,tibble,R,Tidyr,Tibble,我可以将列表列直接卸载到n列中吗 可以假定列表是规则的,所有元素长度相等 如果不是列表列而是字符向量,我可以tidyr::separate。我可以tidyr::unnest,但我们需要另一个助手变量才能tidyr::spread。我是否错过了一个明显的方法 示例数据: library(tibble) df1 <- data_frame( gr = c('a', 'b', 'c'), values = list(1:2, 3:4, 5:6) ) 使用data.table非常简单:

我可以将列表列直接卸载到n列中吗

可以假定列表是规则的,所有元素长度相等

如果不是列表列而是字符向量,我可以
tidyr::separate
。我可以
tidyr::unnest
,但我们需要另一个助手变量才能
tidyr::spread
。我是否错过了一个明显的方法

示例数据:

library(tibble)

df1 <- data_frame(
  gr = c('a', 'b', 'c'),
  values = list(1:2, 3:4, 5:6)
)

使用
data.table
非常简单:

library("data.table")
setDT(df1)
df1[, c("V1", "V2") := transpose(values)]
df1
#    gr values V1 V2
# 1:  a    1,2  1  2
# 2:  b    3,4  3  4
# 3:  c    5,6  5  6
也许是这样:

cbind(df1[, "gr"], do.call(rbind, df1$values))
库(TIBLE)
df1%
变异(r=map(值,~data.frame(t()))%>%
unnest(r)%>%
选择(-value)
##tibble:3 x 3
#gr X1 X2
#     
#1A 12
#2 b 3 4
#3 c 5 6
另一个:

library(tibble)
library(dplyr)

df1 <- data_frame(
  gr = c('a', 'b', 'c'),
  values = list(1:2, 3:4, 5:6)
)

df %>% mutate(V1 = sapply(values, "[[", 1), V2 = sapply(values, "[[", 2))

# A tibble: 3 x 4
  gr    values       V1    V2
  <chr> <list>    <int> <int>
1 a     <int [2]>     1     2
2 b     <int [2]>     3     4
3 c     <int [2]>     5     6

我有过好几次类似的问题。与其他答案相比,我的解决方案无疑是笨拙的,但出于完整性考虑,报告它

library(tibble)
df1 <- data_frame(
  gr = c('a', 'b', 'c'),
  values = list(1:2, 3:4, 5:6)
)

matrix(unlist(df1[1])) -> grs
matrix(unlist(df1[2]), byrow=T, ncol=2) -> vals
使用tidyr 1.0.0,您只需要:

library(tidyr)
df1新名称:
#> * `` -> ...1
#> * `` -> ...2
#>新名称:
#> * `` -> ...1
#> * `` -> ...2
#>新名称:
#> * `` -> ...1
#> * `` -> ...2
#>#tibble:3 x 3
#>gr…1…2
#>     
#>1A 12
#>2 b 3 4
#>3 c 5 6
由(v0.3.0)于2019年9月14日创建

这里的输出是详细的,因为没有水平放置的元素(向量元素)没有命名,而且
unnest\u wider
不想静默猜测

为了避免这种情况,我们可以提前命名:

df1%>%
dplyr::mutate(value=purrr::map(value,setNames,c(“V1”,“V2”)))%%>%
unnest_加宽(值)
#>#tibble:3 x 3
#>gr V1 V2
#>     
#>1A 12
#>2 b 3 4
#>3 c 5 6

或者只使用
suppressMessages()
purrr::quietly()

我不知道为什么,但这样行吗<代码>库(splitstackshape);cSplit(df1,“values”,sep=“:”?@RonakShah我认为引擎盖下的splitstackshape做的就是Victorp的答案。当列表元素长度不相等时的替代方法:
df1[,rn:=.I][,transpose(values),by=(gr,rn)][
谢谢,但我们绝对不想转换成矩阵,把所有东西都强制转换成字符!在未来的版本中,如果我忘记编辑答案,请有人点击我这里!如果我有多个列要取消测试,该怎么办?
library("data.table")
setDT(df1)
df1[, c("V1", "V2") := transpose(values)]
df1
#    gr values V1 V2
# 1:  a    1,2  1  2
# 2:  b    3,4  3  4
# 3:  c    5,6  5  6
cbind(df1[, "gr"], do.call(rbind, df1$values))
library(tibble)

df1 <- data_frame(
  gr = c('a', 'b', 'c'),
  values = list(1:2, 3:4, 5:6)
)

library(tidyverse)

df1 %>%
  mutate(r = map(values, ~ data.frame(t(.)))) %>%
  unnest(r) %>%
  select(-values)

# # A tibble: 3 x 3
#   gr       X1    X2
#   <chr> <int> <int>
# 1 a         1     2
# 2 b         3     4
# 3 c         5     6
library(tibble)
library(dplyr)

df1 <- data_frame(
  gr = c('a', 'b', 'c'),
  values = list(1:2, 3:4, 5:6)
)

df %>% mutate(V1 = sapply(values, "[[", 1), V2 = sapply(values, "[[", 2))

# A tibble: 3 x 4
  gr    values       V1    V2
  <chr> <list>    <int> <int>
1 a     <int [2]>     1     2
2 b     <int [2]>     3     4
3 c     <int [2]>     5     6
library(tibble)
library(dplyr)
library(lazyeval)
df <- data_frame(gr = c('a', 'b', 'c'), values = list(1:11, 3:13, 5:15))
nums <- c(1:11)
ll <- lapply(nums, function(nr) f_interp(~sapply(values, "[[", uq(nr))))
mutate_(df, .dots=setNames(ll, paste("V", nums, sep="")))

# A tibble: 3 x 12
  gr    values        V1    V2    V3    V4    V5    V6    V7    V8    V9   V10
  <chr> <list>     <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 a     <int [11]>     1     2     3     4     5     6     7     8     9    10
2 b     <int [11]>     3     4     5     6     7     8     9    10    11    12
3 c     <int [11]>     5     6     7     8     9    10    11    12    13    14
library(tibble)
df1 <- data_frame(
  gr = c('a', 'b', 'c'),
  values = list(1:2, 3:4, 5:6)
)

matrix(unlist(df1[1])) -> grs
matrix(unlist(df1[2]), byrow=T, ncol=2) -> vals
> data.frame(grs, vals)
  grs X1 X2
1   a  1  2
2   b  3  4
3   c  5  6