无法将新数据合并到列表的每个数据框元素

问题描述 投票:2回答:2

我很难使用for循环将新数据附加到列表的每个数据框元素。

如果我有两个数据框列表(文件列表),我希望“dplyr :: left_join”或“合并”列表中的每个数据框与来自单个数据帧的其他数据,它似乎没有出现在列表中之后。但是,如果我对列表的每个数据框元素逐步和单独使用相同的命令,我会得到相同的警告(由于缺少因子级别),但是得到了所需的结果。例如:

some data frames

df1 <- data.frame(x = 1:3, y=letters[1:3])
df2 <- data.frame(x = 1:5, y=letters[1:5])

# make list of dataframes
filelist <- list(df1,df2)

# new data frame to add to the data frames in the list by indexing "y"
df3 <- data.frame(animal = c(rep("snake", 7)), y=letters[1:7], geno = c("aa", "ab", "ac", "aa", "ac", "ab", "ae"))

# merge df3 into both data frames in the filelist
for (i in 1:length(filelist)) {dplyr::left_join(filelist[[i]], df3, by = "y")}

## Gives the following warning because some factor levels are missing between datasets
Warning message:
Column `y` joining factors with different levels, coercing to character vector 

returned result is the same as the original filelist

> filelist
[[1]]
  x y
1 1 a
2 2 b
3 3 c

[[2]]
  x y
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e

The expected result (done by merging each element of the list separately, then making a new list)

new1 <- dplyr::left_join(filelist[[1]], df3, by = "y")
new2 <- dplyr::left_join(filelist[[2]], df3, by = "y")
newlist <-(new1,new2)
> newlist
[[1]]
  x y animal geno
1 1 a  snake   aa
2 2 b  snake   ab
3 3 c  snake   ac

[[2]]
  x y animal geno
1 1 a  snake   aa
2 2 b  snake   ab
3 3 c  snake   ac
4 4 d  snake   aa
5 5 e  snake   ac

如果不将每个数据框从原始列表中删除,添加新数据,然后创建新列表,最好的方法是什么?

r merging-data
2个回答
0
投票

如警告信息中所述,因素具有不同的级别。

您可以将因子转换为每个数据帧的字符,如下所示:dplyr

df %>% mutate_if(is.factor, as.character) -> df

或者变量y的因子水平均匀化:

for (i in 1:length(filelist)) {
  x = factor(unique(c(levels(filelist[[i]]$y),levels(df3$y))))
  levels(filelist[[i]]$y) = x
  levels(df3$y) = x
  filelist[[i]] = dplyr::left_join(filelist[[i]], df3, by = "y")
}

0
投票

我会使用map包中的purrr函数,就像dplyr是tidyverse的一部分一样:

library(tidyverse)
library(purrr) # loaded when you call tidyverse, but doing it explicitly here

map(filelist, left_join, df3)

[[1]]
  x y animal geno
1 1 a  snake   aa
2 2 b  snake   ab
3 3 c  snake   ac

[[2]]
  x y animal geno
1 1 a  snake   aa
2 2 b  snake   ab
3 3 c  snake   ac
4 4 d  snake   aa
5 5 e  snake   ac

Warning messages:
1: Column `y` joining factors with different levels, coercing to character vector 
2: Column `y` joining factors with different levels, coercing to character vector 
© www.soinside.com 2019 - 2024. All rights reserved.