是否可以取消list()列出的data.frame,同时保留data.frame中的其他列?

时间:2019-06-10 18:59:08

标签: r dataframe unnest

我有一个由其他函数创建的data.frame,这些函数接受.xlsx文件的列表,并读取所有工作簿和包含的工作表。

结果就是这样一个不错的数据框:

df<-data.frame(
file.name <-c(“C:/R/Folder1/WB1.xlsx”,
“C:/R/Folder1/WB2.xlsx,”,”C:/R/Folder1/WB2.xlsx”, “C:/R/Folder2/WB1.xlsx”, “C:/R/Folder2/WB1.xls”),
data<-list(df1,df2,df3,df4,df5))

虽然我能够检索(嵌套)数据帧,但我丢失了所需的相应文件位置。

在取消嵌套时,是否有一种方法可以将数据框中的相应行和paste()保留为一列?

*很抱歉输入错误。发表在SO应用程序上。

  

更新(现在我在PC前面)

可复制的示例:

数据:

df1<-data.frame(V1=c(sample(900:970,6)),
                V2=c(sample(LETTERS[1:6],6)))

df2<-data.frame(V1=sample(750:780,6),
                V2=sample(LETTERS[8:16],6))

df3<-data.frame(V1=sample(200:250,6),
                V2=sample(LETTERS[10:20],6),
                V3=sample(2300:5821,6))

df4<-data.frame(V1=sample(396:480,6),
                V2=sample(LETTERS,6))

df5<-data.frame(V1=sample(50:100,6),
                V2=sample(LETTERS,6))

df6<-data.frame(V1=sample(200:250,6),
                V2=sample(LETTERS,6),
                V3=sample(letters,6))

my.list <- list(df1,df2,df3,df4,df5,df6)

mydf<-data.frame(
  files=c("C:/Folder1/Data/File1.xlsx","C:/Folder1/Data/File2.xlsx",
          "C:/Folder1/Data/File3.xlsx","C:/Folder2/Data/File1.xlsx",
          "C:/Folder2/Data/File2.xlsx","C:/Folder2/Data/File3.xlsx"))

mydf$data<-my.list

尝试进行嵌套时-由于出现了data.frames列表中的观察值和变量不同(第2列),我遇到了以下问题:

y<-unnest(mydf, data)
Error: Column `V3` can't be converted from integer to factor
In addition: Warning messages:
1: In bind_rows_(x, .id) : Unequal factor levels: coercing to character
2: In bind_rows_(x, .id) :
  binding character and factor vector, coercing into character vector
3: In bind_rows_(x, .id) :
  binding character and factor vector, coercing into character vector...
  

其他功能的结果

#tidyverse
y<-mydf %>% unnest(data)
Error: Column `V3` can't be converted from integer to factor
In addition: Warning messages:
1: In bind_rows_(x, .id) : Unequal factor levels: coercing to character

y<-mydf %>%
+   unnest(data) %>%
+   group_by(files) %>%
+   mutate(
+     data = flatten_chr(data),
+     data_colname = str_c("data_", row_number())
+   ) %>% # or just as.character
+   spread(data_colname, data)
Error: Column `V3` can't be converted from integer to factor
In addition: Warning messages:
1: In bind_rows_(x, .id) : Unequal factor levels: coercing to character
  

添加利用功能将.xlsx和所有工作表拉入-如example所示:

library(tidyverse)
library(readxl)

dir_path1 <- "~/File1/Data/Qtr1"  
dir_path2 <- "~/File1/Data/Qtr2"         
dir_path3 <- "~/File1/Data/Qtr3"  
dir_path4 <- "~/File1/Data/Qtr4"

re_file <- ".xlsx"     

read_sheets <- function(dir_path1, file){
  xlsx_file <- paste0(dir_path1, file)
  xlsx_file %>%
    excel_sheets() %>%
    set_names() %>%
    map_df(read_excel, path = xlsx_file, .id = 'sheet_name') %>% 
    mutate(file_name = file) %>% 
    select(file_name, sheet_name, everything())
}

df <- list.files(dir_path, re_file) %>% 
  map_df(~ read_sheets(dir_path, .))

返回:

# A tibble: 15 x 5
   file_name  sheet_name  col1  
   <chr>      <chr>      <dbl> 
 1 Q1_File1.xlsx Sheet1    1         
 2 Q1_File1.xlsx Sheet2    1         
 3 Q1_File2.xlsx Sheet1    1          
 ...

但是,与示例数据不同(如链接中所示),返回的数据(col1)是数据帧的列表。

1 个答案:

答案 0 :(得分:1)

问题与df3的数字代表V3而df6的数字代表V3有关。您可以:

  1. 跳过导入df3$V3df6$V3
  2. 重命名这些变量之一

此外,要消除警告,您可以使用stringsAsFactors = FALSE创建data.frame,也可以使用tibble()代替data.frame(),因为这是小标题的默认行为。

编辑:要更好地执行选项2,可以使用下面的代码为每个变量添加前缀。

my.list2 <- lapply(my.list, function(x) sapply(x, function(y) paste0(class(y), names(y))))
       , function(x) 
         {
         x%>%
           rename_if(is.numeric, ~paste0('num', .x))%>%
           rename_if(is.character, ~paste0('char', .x))%>%
           rename_if(is.factor, ~paste0('fact', .x))
         }
       )

这是选项2,仅适用于因子警告:

df1<-data.frame(V1=c(sample(900:970,6)),
                V2=c(sample(LETTERS[1:6],6)))

df2<-data.frame(V1=sample(750:780,6),
                V2=sample(LETTERS[8:16],6))

df3<-data.frame(V1=sample(200:250,6),
                V2=sample(LETTERS[10:20],6),
                V4=sample(2300:5821,6)) #used to be V3

df4<-data.frame(V1=sample(396:480,6),
                V2=sample(LETTERS,6))

df5<-data.frame(V1=sample(50:100,6),
                V2=sample(LETTERS,6))

df6<-data.frame(V1=sample(200:250,6),
                V2=sample(LETTERS,6),
                V3=sample(letters,6))

my.list <- list(df1,df2,df3,df4,df5,df6)

mydf<-data.frame(
  files=c("C:/Folder1/Data/File1.xlsx","C:/Folder1/Data/File2.xlsx",
          "C:/Folder1/Data/File3.xlsx","C:/Folder2/Data/File1.xlsx",
          "C:/Folder2/Data/File2.xlsx","C:/Folder2/Data/File3.xlsx"))

mydf$data<-my.list

unnest(mydf, data)

                        files  V1 V2   V4   V3
1  C:/Folder1/Data/File1.xlsx 951  A   NA <NA>
2  C:/Folder1/Data/File1.xlsx 932  F   NA <NA>
3  C:/Folder1/Data/File1.xlsx 908  B   NA <NA>
4  C:/Folder1/Data/File1.xlsx 953  C   NA <NA>
5  C:/Folder1/Data/File1.xlsx 929  E   NA <NA>
6  C:/Folder1/Data/File1.xlsx 928  D   NA <NA>
7  C:/Folder1/Data/File2.xlsx 778  K   NA <NA>
8  C:/Folder1/Data/File2.xlsx 771  H   NA <NA>
9  C:/Folder1/Data/File2.xlsx 757  M   NA <NA>
10 C:/Folder1/Data/File2.xlsx 773  P   NA <NA>
11 C:/Folder1/Data/File2.xlsx 759  N   NA <NA>
12 C:/Folder1/Data/File2.xlsx 765  O   NA <NA>
13 C:/Folder1/Data/File3.xlsx 236  M 3964 <NA>
14 C:/Folder1/Data/File3.xlsx 214  O 5241 <NA>
...truncated