简体   繁体   English

如何在 R 中添加一个列,其值引用不同数据框中的列?

[英]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_1sample_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.

相关问题 我想从R数据框中的一列生成5个名称的组合,其在不同列中的值加起来等于或小于一定数量 - I want to generate combinations of 5 names from a column in an R data frame, whose values in a different column add up to a certain number or less 如何在数据框中添加一列,以使添加的值增加R中的总行数? - How can I add a column to a data frame such that the values added increase the total row count in R? 如何在R中使用值列和标签列可视化data.frame? - How can I visualize a data.frame with a values column and a label column in R? 如何在 R 的数据框中添加新列并使用现有列? - How can I add a new column and use an existing column in a data frame in R? 如何在R的数据框中添加不同长度的列? - How to add a column of different length to a data frame in R? 如何在R中的数据框中为列值添加括号? - How to add parentheses to column values in a data frame in R? 如何将一列添加到根据另一列中的值向上计数的数据框中? - How can I add a column to a data frame that counts upwards based on the values in another column? R-如何将数据框的列添加为另一个数据框的列? - R - How to add column of data frame as column of another data frame? R将数据帧的不同列添加到单个列 - R add different column of a data frame to a single column 根据 R 中的列值在数据框末尾添加一列 - Add a column at the end of a data frame based on the column values in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM