[英]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.所以我有一个 R 脚本来对大学橄榄球队进行排名。 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."我最初在 mutate() function 中有样本(随机数),但出现错误“必须是向量,而不是formula
object”。 So I moved it outside the mutate() function and added the as.vector(), but it still gave me the same error.所以我将它移到了 mutate() function 之外并添加了 as.vector(),但它仍然给了我同样的错误。 I also got a warning about "longer object length is not a multiple of shorter object length".我还收到有关“较长的 object 长度不是较短 object 长度的倍数”的警告。 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. case_when
要求所有 arguments 的长度相同。 sample_1
or sample_2
have a length of 1 and it can get recycled. sample_1
或sample_2
的长度为 1,它可以被回收。 ( as.vector
is not needed as rnorm
returns a vector
). ( as.vector
不需要,因为rnorm
返回一个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).此外,当我们使用==
时,它是逐元素比较,并且只能在比较的两列的长度相同或其中一列的长度为 1 时使用(即它被回收)。 Thus Ratings$team == home_team
would be the cause of longer object length
warning.因此Ratings$team == home_team
将导致longer object length
警告。
Instead of case_when
, this maybe done with a join (assuming the 'team' column in 'Ratings' is not duplicated)而不是case_when
,这可以通过加入来完成(假设 'Ratings' 中的 'team' 列不重复)
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.