Adding a new column using other variables

Elia Source

I would like to add a new column named "league" to my sample data based on the names of the players clubs. Of course, my database consists of one thousand records.

My data frame:

df <- read.table(text= " Club       Player
                 'Real Madrid CF'     Ronaldo 
                 'FC Barcelona'       Messi 
                 'FC Bayern München'  Kross 
                 'Club Atlético de Madrid'  Torres
                 Juventus           Dybala
                 'Paris Saint-Germain'       Neymar",
                 header=TRUE)

My idea:

df$League <- ifelse(df$Club == "Real Madrid CF", "Spain",
                     ifelse(df$Club == "FC Barcelona", "Spain",
                     ifelse(df$Club == "Club Atlético de Madrid", "Spain",       
                     ifelse(df$Club == "Juventus", "Italy","Germany"))))

However, it is too time-consuming, I would also like to include Na for other players from outside these leagues.

rdataframe

Answers

answered 2 months ago akrun #1

We can create a named vector ('key/val') and then do the replacement by matching the keys with the 'Club' elements

df$League <- nm1[as.character(df$Club)]
df$League[is.na(df$League)] <- "Germany"

Or another option is to create a key/val dataset, do a left_join

data

nm1 <- structure(c("Spain", "Spain", "Spain", "Italy"), 
  .Names = c("Real Madrid CF", 
 "FC Barcelona", "Club Atlético de Madrid", "Juventus"))

comments powered by Disqus