需要将数据从df1转换为df2吗?
a <- c("New Zealand","Afghanistan","Afghanistan" , "New Zealand", "Afghanistan", "Australia" )
b <- c("Sri Lanka", "Zimbabwe" , "Zimbabwe", "Sri Lanka", "Zimbabwe" , "India" )
d <- c("no result" , "Zimbabwe" , "Zimbabwe" ,"New Zealand", "Afghanistan", "Australia" )
df1 <- data.frame("Team1" = a, "Team2" = b, "Winner" = d)
Country <- c("New Zealand", "Sri Lanka","Afghanistan","Zimbabwe", "Australia","India" )
Match <- c(2,2,3,3,1,1)
Win <- c(1,0,1,2,1,0)
Loss <- c(0,1,2,1,0,1)
Draw <- c(1,1,0,0,0,0)
df2 <- data.frame(Country, Match,Win, Loss, Draw )
谢谢。
答案 0 :(得分:2)
这是一个使用<body>
<li ><a class="waves-effect waves-light btn" id='button' style="top:0px; right:0px; float:right">button</a>
<div class = "toolbar" id ="toolbar">
<ul class="tabs" style="top:-40px">
<li class="tab col s12 l2"><a href="#test1">Test 1</a></li>
<li class="tab col s12 l2">
<a class="active" href="#test2">Test 2</a>
</li>
<li class="tab col s12 l2 disabled">
<a class=active>Disabled Tab</a>
</li>
<li class="tab col s12 l2"><a href="#test4">Test 4</a></li>
</li>
</ul>
</div>
<script>
const button = document.getElementById('button');
const toolbar = document.getElementById('toolbar');
button.onclick = function(){
if(toolbar.classList.contains('toolbar')){
toolbar.classList.remove('toolbar');
toolbar.classList.toggle('toolbar2');
}
else{
toolbar.classList.remove('toolbar2');
toolbar.classList.toggle('toolbar');
}
};
</script>
<script type="text/javascript" src="js/materialize.min.js"></script>
的粗略概念:
raise PermissionDenied("Custom exception message")
答案 1 :(得分:0)
使用dplyr的结果相同
library(tidyverse)
a <- c("New Zealand","Afghanistan","Afghanistan" , "New Zealand", "Afghanistan", "Australia" )
b <- c("Sri Lanka", "Zimbabwe" , "Zimbabwe", "Sri Lanka", "Zimbabwe" , "India" )
d <- c("no result" , "Zimbabwe" , "Zimbabwe" ,"New Zealand", "Afghanistan", "Australia" )
df1 <- data.frame("Team1" = a, "Team2" = b, "Winner" = d, stringsAsFactors = FALSE)
df1 %>%
gather(Team1, Team2, key = Team, value = Country) %>%
mutate(Result = replace(ifelse(Country == Winner, "Win", "Loss"), Winner == "no result", "Draw")) %>%
group_by(Country, Result) %>%
summarise(count = n()) %>%
spread(key = Result, value = count, fill = 0) %>%
mutate(Match = Win + Loss + Draw) %>%
select(Country, Match, Win, Loss, Draw)
# A tibble: 6 x 5
# Groups: Country [6]
Country Match Win Loss Draw
<chr> <dbl> <dbl> <dbl> <dbl>
1 Afghanistan 3 1 2 0
2 Australia 1 1 0 0
3 India 1 0 1 0
4 New Zealand 2 1 0 1
5 Sri Lanka 2 0 1 1
6 Zimbabwe 3 2 1 0
答案 2 :(得分:-1)
这是使用dplyr的方法
tableresults <- function(team,df) {
require(tidyverse)
df2 <- df %>%
filter(Team1 == team | Team2 == team) %>%
mutate(win = ifelse(Winner == team,1,0),
draw = ifelse(Winner == 'no result',1,0),
loss = ifelse(!Winner %in% c('no result',team),1,0),
country = team) %>%
group_by(country) %>%
summarize(match = n(),
win = sum(win),
loss = sum(loss),
draw = sum(draw)) %>%
ungroup()
return(df2)
}
countries <- df1 %>% distinct(Team1,Team2) %>% gather() %>% pull(value)
results_tbl <- tibble()
for (i in 1:length(countries)) {
country_tbl <- tableresults(countries[[i]],df1)
results_tbl <- bind_rows(results_tbl,country_tbl)
}
结果:
> results_tbl
# A tibble: 6 x 5
country match win loss draw
<chr> <int> <dbl> <dbl> <dbl>
1 New Zealand 2 1 0 1
2 Afghanistan 3 1 2 0
3 Australia 1 1 0 0
4 Sri Lanka 2 0 1 1
5 Zimbabwe 3 2 1 0
6 India 1 0 1 0