将数据框列的列表映射到另一个数据框 [英] Map a list of dataframe columns into another dataframe r

查看:49
本文介绍了将数据框列的列表映射到另一个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是数据帧的示例列表. listofDataFrames 包含多个数据帧.每个数据框包含一列 lev ,这是在映射过程中使用的键.值是除 lev 以外的列.应基于 listofDataFrames 的映射为 DF 生成新列.更清楚地说,如果我们考虑 listofDataFrames 中的 colors ,则有两列:"colors number 3"(颜色数3)和"colors number 3"(颜色数3).和"10号颜色".这些列均包含3个唯一值:和?".在 DF 中,我们应该创建两个新列:"colors number 3"(颜色3)和"10号颜色".我们可以根据 listofDataFramescolors 中的 lev 列创建它们.在 DF`中,如果是针对特定的行和列,则颜色"为有橙色"那么我们应该将"r"映射到用于新列"3号颜色".预期输出如下.

An example list of dataframes are given below. listofDataFrames contains multiple dataframes. Each dataframe contains a column lev which is the key to be used in the mapping process. The values are the columns except lev. New columns should be generated for DF based on mapping from listofDataFrames. To be more clear, if we consider colors from listofDataFrames, there are two columns: "colors number 3" and "colors number 10". These columns both contain 3 unique values : "r","l" and "?". In DF we should create two new columns: "colors number 3" and "colors number 10". We can create them based on the lev column in colors from listofDataFrames. In DF` if for a particular row and column "colors" has "orange" then we should map "r" for the new column "colors number 3". The expected output is given below.

# 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
           )

推荐答案

在这里,我们可以先在'流派'上使用双 merge ,然后在'DF'的'colors'列上使用相应的 list 元素

Here, we could use double merge first on the 'genres' and then on the 'colors' column of 'DF' with corresponding list elements

merge(merge(DF, listofDataFrames[['genres']], all.x = TRUE, 
   by.x = 'genres', by.y = 'lev'), 
     listofDataFrames[['colors']], all.x = TRUE, by.x = 'colors', by.y = 'lev')


或者我们可以使用循环


Or we can use a loop

nm1 <- names(listofDataFrames)
out <- DF
for(i in seq_along(nm1)) {
     out <- merge(out, listofDataFrames[[nm1[i]]], all.x = TRUE,
       by.x = nm1[i], by.y = 'lev')
}

这篇关于将数据框列的列表映射到另一个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆