我想知道我有多少低、中、高的戏剧,以及我的数据框架中有多少个低、中、高的犯罪。
下面是我的数据框架的一个示例:
genres class_rentabilite
Crime, Drama Medium
Action, Crime, Drama, Thriller High
Action, Adventure, Sci-Fi, Thriller Medium
Drama Low
Crime, Drama High
Comedy, Drama high我在数据中的另一列中使用了table(),它起了作用:
table(df$language, df$class_rentabilite)上面的代码给出了如下内容:
Low Medium High NA
1 1 0 3
Aboriginal 0 0 2 0
Arabic 0 0 1 3
Aramaic 1 0 0 0
Bosnian 1 0 0 0
Cantonese 5 2 1 3我想对示例数据使用这种方法,但是table()不能工作,因为我在genres的每一行中都有多个值。我怎样才能解决这个问题?
发布于 2016-12-12 01:01:28
这是给你的一个方法。您可以使用separate_rows()拆分类型并创建一个临时数据框架。然后,您可以像以前一样使用table()。
library(dplyr)
library(tidyr)
mydf %>%
separate_rows(genres, sep = ", ") -> foo
table(foo$genres, foo$class_rentabilite)
# High Low Medium
# Action 1 0 1
# Adventure 0 0 1
# Comedy 1 0 0
# Crime 2 0 1
# Drama 3 1 1
# Sci-Fi 0 0 1
# Thriller 1 0 1数据
mydf <- structure(list(genres = c("Crime, Drama", "Action, Crime, Drama, Thriller",
"Action, Adventure, Sci-Fi, Thriller", "Drama", "Crime, Drama",
"Comedy, Drama"), class_rentabilite = c("Medium", "High", "Medium",
"Low", "High", "High")), .Names = c("genres", "class_rentabilite"
), row.names = c(NA, -6L), class = "data.frame")https://stackoverflow.com/questions/41092663
复制相似问题