没有重复的 df 中的频率 R
Frequence in a df without repetition R
你好,我有 df“连衣裙”
type size color
1 t-shirt small blue
2 t-shirt medium blue
3 jeans large yellow
4 skirt small pink
5 skirt medium yellow
6 blouse large blue
7 jeans small blue
8 t-shirt medium blue
9 skirt large yellow
10 blouse large pink
我正在计算颜色是“蓝色”的次数
sum(dress$color == "blue")
结果是5件,其中有3件T恤。我只想知道有多少“类型”是蓝色的,所以如果重复“T 恤”类型,我希望只计算一次。问题是:“在多少种类型中,颜色“蓝色”被重复了?”所以,在这个条件下,结果应该是 2。我该怎么做?
另外,我想知道一个类型被重复了多少次,例如“T恤蓝色被重复了3次”这样的信息。
df 是:
dput(dress)
structure(list(type = c("t-shirt", "t-shirt", "jeans", "skirt",
"skirt", "blouse", "jeans", "t-shirt", "skirt", "blouse"), size = c("small",
"medium", "large", "small", "medium", "large", "small", "medium",
"large", "large"), color = c("blue", "blue", "yellow", "pink",
"yellow", "blue", "blue", "blue", "yellow", "pink"), freqBlue = c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L)), row.names = c(NA, -10L), class = "data.frame")
如果我没有正确理解你的问题,dplyr
就是你要找的。
dt %>%
group_by(type, color) %>%
summarise(
freq =n()
)
其中 dt
是您的 data.frame
,脚本会产生以下输出,
# A tibble: 7 x 3
# Groups: type [4]
type color freq
<chr> <chr> <int>
1 blouse blue 1
2 blouse pink 1
3 jeans blue 1
4 jeans yellow 1
5 skirt pink 1
6 skirt yellow 2
7 t-shirt blue 3
这里n()
按组统计出现次数,可以继续加组!
你好,我有 df“连衣裙”
type size color
1 t-shirt small blue
2 t-shirt medium blue
3 jeans large yellow
4 skirt small pink
5 skirt medium yellow
6 blouse large blue
7 jeans small blue
8 t-shirt medium blue
9 skirt large yellow
10 blouse large pink
我正在计算颜色是“蓝色”的次数
sum(dress$color == "blue")
结果是5件,其中有3件T恤。我只想知道有多少“类型”是蓝色的,所以如果重复“T 恤”类型,我希望只计算一次。问题是:“在多少种类型中,颜色“蓝色”被重复了?”所以,在这个条件下,结果应该是 2。我该怎么做?
另外,我想知道一个类型被重复了多少次,例如“T恤蓝色被重复了3次”这样的信息。
df 是:
dput(dress)
structure(list(type = c("t-shirt", "t-shirt", "jeans", "skirt",
"skirt", "blouse", "jeans", "t-shirt", "skirt", "blouse"), size = c("small",
"medium", "large", "small", "medium", "large", "small", "medium",
"large", "large"), color = c("blue", "blue", "yellow", "pink",
"yellow", "blue", "blue", "blue", "yellow", "pink"), freqBlue = c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L)), row.names = c(NA, -10L), class = "data.frame")
如果我没有正确理解你的问题,dplyr
就是你要找的。
dt %>%
group_by(type, color) %>%
summarise(
freq =n()
)
其中 dt
是您的 data.frame
,脚本会产生以下输出,
# A tibble: 7 x 3
# Groups: type [4]
type color freq
<chr> <chr> <int>
1 blouse blue 1
2 blouse pink 1
3 jeans blue 1
4 jeans yellow 1
5 skirt pink 1
6 skirt yellow 2
7 t-shirt blue 3
这里n()
按组统计出现次数,可以继续加组!