我有一个smp数据框:
loser_ioc winner_ioc
AUT IND
所以我想计算每个国家的出现并使用一个清单:
country <- list()
for (i in 1:Size){
if (is.null(country[[smp[i, "winner_ioc"]]])) {
country[[ smp[i, "winner_ioc"]]] = 1
} else {
country[[ smp[i, "winner_ioc"]]] = country[[ smp[i, "winner_ioc"]]] + 1
}
if (is.null(l[[ smp[i, "loser_ioc"]]])) {
country[[ smp[i, "loser_ioc"]]] = 1
} else {
country[[ smp[i, "loser_ioc"]]] = country[[ smp[i, "loser_ioc"]]] + 1
}
}
countryD <- data.frame(country)
write.csv(countryD, file = "country.csv")
但我的索引越界错误...
Error in country[[smp[i, "winner_ioc"]]] : subscript out of bounds
感谢和问候
答案 0 :(得分:0)
您可以获得使用table(smp)
winner
loser AUD CAD MEX NZD USA
AUD 0 0 0 1 1
CAD 0 1 0 0 2
MEX 3 0 0 1 0
NZD 2 1 0 0 0
USA 0 0 1 0 1
或者可以使用spreading
列,然后grouping
中的counting
和tidyverse
来执行此操作
library(tidyverse)
x <- data.frame(loser = sample(c("AUT", "USA", "NZ", "CAD", "MEX"), 14, replace = TRUE),
winner = sample(c("AUT", "USA", "NZ", "CAD", "MEX"), 14, replace = TRUE))
new_x <- x %>% gather(result, who, c(winner, loser)) %>% group_by_all() %>% count()
> x %>% gather(result, who, c(winner, loser)) %>% group_by_all() %>% count()
# A tibble: 10 x 3
# Groups: result, who [10]
result who n
<chr> <chr> <int>
1 loser AUD 2
2 loser CAD 3
3 loser MEX 4
4 loser NZD 3
5 loser USA 2
6 winner AUD 5
7 winner CAD 2
8 winner MEX 1
9 winner NZD 2
10 winner USA 4