将列表拆分为数据帧R
我正在尝试将此列表拆分为一个长格式的数据帧将列表拆分为数据帧R,r,R,我正在尝试将此列表拆分为一个长格式的数据帧 list_a <- list(`Blue Banana` = 8.7, `Green Strawberry` = 2.3, `Blue Squash` = 3.5, `Orange Cherry` = 4.5) 您可以尝试: library(tidyverse) list_a %>% bind_rows %>% gather %>% separate(col = key, sep
list_a <- list(`Blue Banana` = 8.7, `Green Strawberry` = 2.3,
`Blue Squash` = 3.5, `Orange Cherry` = 4.5)
您可以尝试:
library(tidyverse)
list_a %>%
bind_rows %>%
gather %>%
separate(col = key, sep = " ", c("Color", "Fruit"))
# A tibble: 4 x 3
Color Fruit value
<chr> <chr> <dbl>
1 Blue Banana 8.7
2 Green Strawberry 2.3
3 Blue Squash 3.5
4 Orange Cherry 4.5
库(tidyverse)
列表a%>%
绑定_行%>%
聚集%>%
分离(col=键,sep=“”,c(“颜色”,“水果”))
#一个tibble:4x3
彩色水果价值
1蓝香蕉8.7
2绿色草莓2.3
3蓝南瓜3.5
4橙樱桃4.5
您可以尝试:
library(tidyverse)
list_a %>%
bind_rows %>%
gather %>%
separate(col = key, sep = " ", c("Color", "Fruit"))
# A tibble: 4 x 3
Color Fruit value
<chr> <chr> <dbl>
1 Blue Banana 8.7
2 Green Strawberry 2.3
3 Blue Squash 3.5
4 Orange Cherry 4.5
库(tidyverse)
列表a%>%
绑定_行%>%
聚集%>%
分离(col=键,sep=“”,c(“颜色”,“水果”))
#一个tibble:4x3
彩色水果价值
1蓝香蕉8.7
2绿色草莓2.3
3蓝南瓜3.5
4橙樱桃4.5
您可以使用base R中的read.table()
执行此操作
cbind(
read.table(text=names(list_a), col.names=c("Color", "Fruit")),
value=unlist(list_a, use.names=FALSE)
)
# Color Fruit value
# 1 Blue Banana 8.7
# 2 Green Strawberry 2.3
# 3 Blue Squash 3.5
# 4 Orange Cherry 4.5
或者使用strcapture()
或者在stack()
的帮助下,简单调用tidyr::separate()
您可以使用base R中的
read.table()
执行此操作
cbind(
read.table(text=names(list_a), col.names=c("Color", "Fruit")),
value=unlist(list_a, use.names=FALSE)
)
# Color Fruit value
# 1 Blue Banana 8.7
# 2 Green Strawberry 2.3
# 3 Blue Squash 3.5
# 4 Orange Cherry 4.5
或者使用strcapture()
或者在stack()
的帮助下,简单调用tidyr::separate()
如果要将列表元素分解为多个数据帧,是否可以使用相同的方法?如果要将列表元素分解为多个数据帧,是否可以使用相同的方法?
tidyr::separate(stack(list_a), ind, c("Color", "Fruit"))
# values Color Fruit
# 1 8.7 Blue Banana
# 2 2.3 Green Strawberry
# 3 3.5 Blue Squash
# 4 4.5 Orange Cherry