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,我有一个这样的数据帧 输入 我想将这两列转换为一个列表,如下所示: 输出: $`am_A` [1] "re456" "re4" $`am_B` [1] "re20" "re456" $`am_C` [1] "re47" "re12" 尝试使用pivot\u-wide,然后unest-ing列,最后使用as.list获取列表 库(tidyverse) 我的_df% pivo

我有一个这样的数据帧

输入

我想将这两列转换为一个列表,如下所示:

输出:

$`am_A`
[1] "re456" "re4"

$`am_B`
[1] "re20" "re456"

$`am_C`
[1] "re47" "re12"

尝试使用
pivot\u-wide
,然后
unest
-ing列,最后使用
as.list
获取列表

库(tidyverse)
我的_df%
pivot\u更宽(name\u from=ID,
值_from=RE,
值_fn=list)%>%#以抑制警告
unnest(cols=names(.))%>%#unnest现在需要列名
as.list()
#>$am_A
#>[1]“re456”“re4”
#> 
#>$am_B
#>[1]“re20”“re456”
#> 
#>$am_C
#>[1]“re47”“re12”

由(v0.3.0)于2021年4月13日创建,用于:

your.data%>%dlply(“ID”,“RE”)
输出:

$am_A
[1] "re456" "re4"  

$am_B
[1] "re20"  "re456"

$am_C
[1] "re47" "re12"

attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
    ID
1 am_A
2 am_B
3 am_C
如果您不需要这些额外的属性,可以通过管道连接到c:

data%>%dlply(“ID”,“RE”)%>%c

只需使用
从基R拆分

split(dat$RE, dat$ID)

# $am_A
# [1] "re456" "re4"  
# 
# $am_B
# [1] "re20"  "re456"
# 
# $am_C
# [1] "re47" "re12"
数据:

dat
split(dat$RE, dat$ID)

# $am_A
# [1] "re456" "re4"  
# 
# $am_B
# [1] "re20"  "re456"
# 
# $am_C
# [1] "re47" "re12"
dat <- structure(
  list(
    ID = c("am_A", "am_A", "am_B", "am_C", "am_B", "am_C"),
    RE = c("re456", "re4", "re20", "re47", "re456", "re12")
    ),
  class = "data.frame", row.names = c(NA,-6L)
)