简体   繁体   中英

How can I add a column in R whose values reference a column in a different data frame?

So I have an R script that ranks college football teams. It outputs a rating and I want to take that rating from a different data frame and add it as a new column to a different data frame containing info from the upcoming week of games. Here's what I'm currently trying to do:

random_numbers <- rnorm(130, mean = mean_value, sd = sd_value)
sample_1 <- as.vector(sample(random_numbers, 1, replace = TRUE))
upcoming_games_df <- upcoming_games_df %>%
  mutate(home_rating = case_when(home_team %in% Ratings$team ~ Ratings$Rating[Ratings$team == home_team]),
         TRUE ~ sample_1)
sample_2 <- as.vector(sample(random_numbers, 1, replace = TRUE))
upcoming_games_df <- upcoming_games_df %>%
  mutate(away_rating = case_when(away_team %in% PrevWeek_VoA$team ~ Ratings$Rating[Ratings$team == away_team]),
         TRUE ~ sample_2)

I originally had the sample(random_numbers) inside of the mutate() function but I got error "must be a vector, not a formula object." So I moved it outside the mutate() function and added the as.vector(), but it still gave me the same error. I also got a warning about "longer object length is not a multiple of shorter object length". I don't know what to do now. The code above is the last thing I tried before coming here for help.

case_when requires all arguments to be of same length. sample_1 or sample_2 have a length of 1 and it can get recycled. ( as.vector is not needed as rnorm returns a vector ).

In addition, when we use == , it is elementwise comparison and can be used only when the length of both the columns compared are same or one of them have a length of 1 (ie it gets recycled). Thus Ratings$team == home_team would be the cause of longer object length warning.

Instead of case_when , this maybe done with a join (assuming the 'team' column in 'Ratings' is not duplicated)

library(dplyr)
upcoming_games_df2 <- upcoming_games_df %>%
   left_join(Ratings, by = c("home_team" = "team")) %>%
   mutate(home_rating = coalesce(Rating, sample_1), team = NULL) %>%
   left_join(PrevWeek_VoA, by = c("away_team" = "team")) %>%
   mutate(away_rating = coalesce(Rating, sample_2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM