How to create new columns in a new data frame from information in an existing data frame in R

IAwa

I have an existing data frame with these columns. The home teams played against the Away Team in the sequence mentioned. In Results column H denotes that Home Team won, P denotes AwayTeam won and D denotes that it was a draw.

HomeTeam = Liverpool, Brighton, Birmingham, Manchester, Portsmouth

Away Team = Netherland, Austria, Cambodia, Netherlands, Austria

Results = H,H,P,D,H

My new data frame consists of column 'TeamName' where it shows the total number of teams playing the series.

TeamName = Liverpool, Brighton, Birmingham, Manchester, Netherland, Austria, Cambodia, Portsmouth

I want to add a column in the new data frame named 'Record' that record as wins, losses and ties for each team.

I am new in R so any help will be great! Thanks!

br00t
library(data.table)
df1 <- data.frame(HomeTeam = c('Liverpool', 'Brighton', 'Birmingham', 'Manchester', 'Portsmouth'),
                  AwayTeam = c('Netherlands', 'Austria', 'Cambodia', 'Netherlands', 'Austria'),
                  Results = c('H','H','P','D','H'))
teams <- c(df1$HomeTeam, df1$AwayTeam) |> unique() |> sort()
df2 <- lapply(teams, function(x) {
  wins <- 0
  losses <- 0
  draws <- 0
  idx <- which(df1$HomeTeam == x)
  wins <- sum(df1[ idx, 'Results' ] == 'H')
  losses <- sum(df1[ idx, 'Results' ] == 'P')
  draws <- sum(df1[ idx, 'Results' ] == 'D')
  idx <- which(df1$AwayTeam == x)
  wins <- wins + sum(df1[ idx, 'Results' ] == 'P')
  losses <- losses + sum(df1[ idx, 'Results' ] == 'H')
  draws <- draws + sum(df1[ idx, 'Results' ] == 'D')
  data.frame(TeamName = x, Wins = wins, Losses = losses, Draws = draws)
}) |> 
  rbindlist() |> 
  as.data.frame()
df2 |> print()

Output:

     TeamName Wins Losses Draws
1     Austria    0      2     0
2  Birmingham    0      1     0
3    Brighton    1      0     0
4    Cambodia    1      0     0
5   Liverpool    1      0     0
6  Manchester    0      0     1
7 Netherlands    0      1     1
8  Portsmouth    1      0     0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Splitting a data frame to create new columns

How to create new columns in data.frame on the fly?

Create new data frame based on values from another data frame

How to create a new dataframe with values based on existing data frame and range from numeric vectors in R.

Adding a vector as new columns in existing data frame

Create a new column from different columns of one data frame conditioned on another column from another data frame

Pyspark create new data frame with updating few columns from old data frame

How to create a new column in a data frame depending on multiple criteria from multiple columns from the same data frame

How to remove unmatched data from two data frames, to create a new data frame in R

Creating new new data frame with so many columns from existing dataframe by dropping one column

How to create a new table that summarises data from another data frame?

Combining two columns in a data frame and creating a new column in an existing data frame in R

How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration?

is python possible to create a new data frame from the existing data frame?

Create a new data frame in R containing statistics of another data frame

create a new data frame with columns from another data frame based on column names in R

Creating a new data frame from two existing data frame based on values from two columns

Creating a new data frame with calculations from an existing data frame using R

How can I filter into a new data frame conditional on the columns existing in another data frame?

create a new data frame from existing data frame based on condition

Pivot table in R: create a new data frame and populate it from an existing data frame using logical statements

Create new columns based on count of occurrence in another data frame in r

How can I create a new data frame based on the existing columns?

How to extract information from one column to create a new column in a pandas data frame

R: Create new data frame or Matrix from two data frames

How to create this new dataframe by manipulating existing data frame?

how can I create a new data frame using exact rows from the old data frame in R Studio?

Create new Data frame from an existing one in pyspark

How to create a new column in R from data in another data frame?