我有一个从json导入的字符串数组的列表。
data = fromJSON("data.json")
> data
[[1]]
[1] "Finish" "Accept" "Request"
[[2]]
[1] "Finish" "Accept" "Request"
[[3]]
[1] "Finish" "Accept" "UnlockRequest"
[4] "Accept" "Request"
[[4]]
[1] "Finish" "Accept" "Request"
[[5]]
[1] "Finish" "Accept" "Request"
我现在的目标是计算列表对象,类似于数据表的table()
函数,但是table(data)
返回错误:
table(data)
中的错误:所有参数的长度必须相同
这里正确的解决方法是什么?
输出应类似于:
c("Finish", "Accept", "Request") 4
c("Finish", "Accept", "UnlockRequest", "Accept",
"Request") 1
数据:
list(c("Finish", "Accept", "Request"), c("Finish", "Accept",
"Request"), c("Finish", "Accept", "UnlockRequest", "Accept",
"Request"), c("Finish", "Accept", "Request"), c("Finish", "Accept",
"Request"))
答案 0 :(得分:2)
请注意这是否“正确”,但这是一种方法。基本上将其变成带有一个列表列的data_frame
,操纵列表列(使用as.character
),然后对其计数:
library(tidyverse)
data_frame(l1) %>%
mutate(char = as.character(l1)) %>%
count(char)
# A tibble: 2 x 2
char n
<chr> <int>
1 "c(\"Finish\", \"Accept\", \"Request\")" 4
2 "c(\"Finish\", \"Accept\", \"UnlockRequest\", \"Accept\", \"Request\")" 1
另一种方法是将文本paste
在一起,运行table
,然后使用strsplit
。
table(sapply(l1, paste, collapse = ',')) %>%
as_data_frame() %>%
mutate(list_col = strsplit(Var1, ','))
Source: local data frame [2 x 3]
Groups: <by row>
# A tibble: 2 x 3
Var1 n list_col
<chr> <int> <list>
1 Finish,Accept,Request 4 <chr [3]>
2 Finish,Accept,UnlockRequest,Accept,Request 1 <chr [5]>
答案 1 :(得分:0)
您可以在列表上运行as.character()
,然后将结果制成表格。
as.data.frame(table(as.character(x)))
# Var1 Freq
# 1 c("Finish", "Accept", "Request") 4
# 2 c("Finish", "Accept", "UnlockRequest", "Accept", "Request") 1
数据:
x <- list(c("Finish", "Accept", "Request"), c("Finish", "Accept",
"Request"), c("Finish", "Accept", "UnlockRequest", "Accept",
"Request"), c("Finish", "Accept", "Request"), c("Finish", "Accept",
"Request"))