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

Is it possible to unlist() listed data.frame while keeping other columns from data.frame?

我有一个 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 应用上。

Updating (now that I am in front of a 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.frame 列表(第 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...

Results from other functions

#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

Adding utilized function to pull .xlsx and all sheets in - as seen in this :

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, .))

Returns:

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

然而,与样本数据不同(如link所示),返回的数据(col1)是一个数据帧列表。

此问题与 df3 具有 V3 的数字而 df6 是 V3 的字符有关。您可以:

  1. 跳过导入 df3$V3df6$V3
  2. 重命名其中一个变量

此外,要消除警告,您可以使用 stringsAsFactors = FALSE 创建 data.frames 或者您可以使用 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