【发布时间】:2021-03-23 13:39:00
【问题描述】:
下面给出了数据框的示例列表。 listofDataFrames 包含多个数据帧。每个数据框都包含一列lev,这是映射过程中使用的键。这些值是除lev 之外的列。应根据来自listofDataFrames 的映射为DF 生成新列。更清楚地说,如果我们从listofDataFrames 中考虑colors,则有两列:“colors number 3”和“colors number 10”。这些列都包含 3 个唯一值:“r”、“l”和“?”。在DF 中,我们应该创建两个新列:“colors number 3”和“colors number 10”。我们可以基于colors from listofDataFrames. In DF` 中的lev 列创建它们,如果对于特定的行和列“颜色”具有“橙色”,那么我们应该为新列“颜色编号 3”映射“r” ”。下面给出了预期的输出。
# Create an example list of dataframes and populate it
listofDataFrames <- list()
genres <- data.frame("genres number 12" = c("r","l","?","r","r"),
"genres number 17" = c("l","r","?","l","?"),
lev = c("pop","rock","jazz","blues","r&b"),
check.names = FALSE)
colors <- data.frame("colors number 3" = c("l","r","?","r"),
"colors number 10" = c("l","r","l","r"),
lev = c("red","blue","green","orange"),
check.names = FALSE)
listofDataFrames[["genres"]] <- genres
listofDataFrames[["colors"]] <- colors
## DF
DF <-data.frame("genres" = c("pop", "pop","jazz","rock","jazz","blues","rock","pop","blues","pop"),
"colors" = c("orange","red","red","orange","green","blue","orange","red","blue","green"),
"values" = c(12, 15, 24, 33 ,47, 2 , 9 ,6, 89, 75))
## EXPECTED OUTPUT
expectedOutput <-
data.frame("genres" = c("pop", "pop","jazz","rock","jazz","blues","rock","pop","blues","pop"),
"colors" = c("orange","red","red","orange","green","blue","orange","red","blue","green"),
"values" = c(12, 15, 24, 33 ,47, 2 , 9 ,6, 89, 75),
"genres number 12" = c("r","r","?","l","?","r","l","r","r","r"),
"genres number 17" = c("l","l","?","r","?","l","r","l","l","l"),
"colors number 3" = c("r","l","l","r","?","r","r","l","r","?"),
"colors number 10" = c("r","l","l","r","l","r","r","l","r","l"),
check.names = FALSE
)
【问题讨论】: