Data Wrangling: Reshaping
Need to transform a data, from df1 to 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 )
Thanks in advance.
r dplyr data.table reshape2
add a comment |
Need to transform a data, from df1 to 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 )
Thanks in advance.
r dplyr data.table reshape2
1
What did you try???
– Sotos
Nov 14 '18 at 13:17
add a comment |
Need to transform a data, from df1 to 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 )
Thanks in advance.
r dplyr data.table reshape2
Need to transform a data, from df1 to 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 )
Thanks in advance.
r dplyr data.table reshape2
r dplyr data.table reshape2
edited Nov 14 '18 at 13:18
Xenus
asked Nov 14 '18 at 13:15
XenusXenus
194
194
1
What did you try???
– Sotos
Nov 14 '18 at 13:17
add a comment |
1
What did you try???
– Sotos
Nov 14 '18 at 13:17
1
1
What did you try???
– Sotos
Nov 14 '18 at 13:17
What did you try???
– Sotos
Nov 14 '18 at 13:17
add a comment |
3 Answers
3
active
oldest
votes
Here is a rough concept using data.table:
library(data.table)
df1_melted <- melt(setDT(df1), id.vars = "Winner", value.name = "Country")
df2b <- df1_melted[,
.(Matches = .N,
Win = sum(Winner == Country),
Loss = sum(Winner != Country & Winner != "no result"),
Draw = sum(Winner == "no result")),
by = Country]
df2b
Country Matches Win Loss Draw
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
add a comment |
Same result using 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
add a comment |
Here is a method using 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:
> 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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301125%2fdata-wrangling-reshaping%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a rough concept using data.table:
library(data.table)
df1_melted <- melt(setDT(df1), id.vars = "Winner", value.name = "Country")
df2b <- df1_melted[,
.(Matches = .N,
Win = sum(Winner == Country),
Loss = sum(Winner != Country & Winner != "no result"),
Draw = sum(Winner == "no result")),
by = Country]
df2b
Country Matches Win Loss Draw
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
add a comment |
Here is a rough concept using data.table:
library(data.table)
df1_melted <- melt(setDT(df1), id.vars = "Winner", value.name = "Country")
df2b <- df1_melted[,
.(Matches = .N,
Win = sum(Winner == Country),
Loss = sum(Winner != Country & Winner != "no result"),
Draw = sum(Winner == "no result")),
by = Country]
df2b
Country Matches Win Loss Draw
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
add a comment |
Here is a rough concept using data.table:
library(data.table)
df1_melted <- melt(setDT(df1), id.vars = "Winner", value.name = "Country")
df2b <- df1_melted[,
.(Matches = .N,
Win = sum(Winner == Country),
Loss = sum(Winner != Country & Winner != "no result"),
Draw = sum(Winner == "no result")),
by = Country]
df2b
Country Matches Win Loss Draw
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
Here is a rough concept using data.table:
library(data.table)
df1_melted <- melt(setDT(df1), id.vars = "Winner", value.name = "Country")
df2b <- df1_melted[,
.(Matches = .N,
Win = sum(Winner == Country),
Loss = sum(Winner != Country & Winner != "no result"),
Draw = sum(Winner == "no result")),
by = Country]
df2b
Country Matches Win Loss Draw
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
edited Nov 14 '18 at 13:46
answered Nov 14 '18 at 13:35
sindri_baldursindri_baldur
7,655932
7,655932
add a comment |
add a comment |
Same result using 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
add a comment |
Same result using 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
add a comment |
Same result using 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
Same result using 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
answered Nov 14 '18 at 13:44
Jordo82Jordo82
59218
59218
add a comment |
add a comment |
Here is a method using 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:
> 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
add a comment |
Here is a method using 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:
> 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
add a comment |
Here is a method using 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:
> 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
Here is a method using 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:
> 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
answered Nov 14 '18 at 13:43
Randall HelmsRandall Helms
537210
537210
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301125%2fdata-wrangling-reshaping%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What did you try???
– Sotos
Nov 14 '18 at 13:17