我对r很陌生,想根据其他两个变量来计算变量的百分比。我的数据的简化版本是:
choice g score
1 M .10
1 M .20
1 F .15
1 F .15
1 M .20
2 M .05
2 M .05
2 M .15
2 F .20
我正在寻找的结果是:
choice score %g(M) %g(F)
1 .10 .333 0
1 .15 0 1
1 .20 .667
2 .05 .667 0
2 .15 .333 0
2 .20 0 1
我希望这很清楚。任何帮助,将不胜感激!谢谢。
答案 0 :(得分:1)
使用tidyverse
软件包的解决方案。关键是计算不同组列的行数,计算百分比,然后展开数据框。
library(tidyverse)
dat2 <- dat %>%
group_by(choice, g) %>%
add_count() %>%
group_by(choice, g, score) %>%
add_count() %>%
mutate(Percentage = nn/n) %>%
select(-n, -nn) %>%
distinct() %>%
spread(g, Percentage, fill = 0) %>%
select(choice, score, `%g(M)` = M, `%g(F)` = F) %>%
ungroup()
dat2
# # A tibble: 6 x 4
# choice score `%g(M)` `%g(F)`
# <int> <dbl> <dbl> <dbl>
# 1 1 0.1 0.333 0
# 2 1 0.15 0 1
# 3 1 0.2 0.667 0
# 4 2 0.05 0.667 0
# 5 2 0.15 0.333 0
# 6 2 0.2 0 1
或者以下内容,比我以前的解决方案更简洁。
dat2 <- dat %>%
count(choice, g, score) %>%
group_by(choice, g) %>%
mutate(Percentage = n/sum(n)) %>%
select(-n) %>%
spread(g, Percentage, fill = 0) %>%
select(choice, score, `%g(M)` = M, `%g(F)` = F) %>%
ungroup()
dat2
# # A tibble: 6 x 4
# choice score `%g(M)` `%g(F)`
# <int> <dbl> <dbl> <dbl>
# 1 1 0.1 0.333 0
# 2 1 0.15 0 1
# 3 1 0.2 0.667 0
# 4 2 0.05 0.667 0
# 5 2 0.15 0.333 0
# 6 2 0.2 0 1
数据
dat <- read.table(text = "choice g score
1 M .10
1 M .20
1 F .15
1 F .15
1 M .20
2 M .05
2 M .05
2 M .15
2 F .20",
header = TRUE, stringsAsFactors = FALSE)
答案 1 :(得分:1)
这是使用<dl>
<dt>aluminum
<button type=button id=expand-aluminum><abbr title="See Definition"><i>i</i></abbr></button>
<dd id=aluminum-definition>the chemical element of atomic number 13, a light silvery-grey metal.
<dt>silver
<button type=button id=expand-silver><abbr title="See Definition"><i>i</i></abbr></button>
<dd id=silver-definition>a precious shiny greyish-white metal, the chemical element of atomic number 47.
<dt>gold
<button type=button id=expand-gold><abbr title="See Definition"><i>i</i></abbr></button>
<dd id=gold-definition>a yellow precious metal, the chemical element of atomic number 79.
<dt>platinum
<button type=button id=expand-platinum><abbr title="See Definition"><i>i</i></abbr></button>
<dd id=platinum-definition>a precious silvery-white metal, the chemical element of atomic number 78.
</dl>
的解决方案。基本上,OP会在百分比中寻找类似列联表的内容。 data.table
函数在这里很有用:
table
输出:
#convert into a factor
dat[, g := as.factor(g)]
#count number of M/F for each choice and g
dat[, nMF := .N, by=.(choice, g)]
#tabulate the observations and divide by number of M/F
dat[, as.list(table(g) / nMF), by=.(choice, score)]
数据:
choice score F M
1: 1 0.10 0 0.3333333
2: 1 0.20 0 0.6666667
3: 1 0.15 1 0.0000000
4: 2 0.05 0 0.6666667
5: 2 0.15 0 0.3333333
6: 2 0.20 1 0.0000000