【问题标题】:Importing multiple .csv files into R with differents names将多个 .csv 文件导入具有不同名称的 R
【发布时间】:2019-03-28 14:23:41
【问题描述】:

我有一个包含 37 个 Locations.csv 和 37 个 Behavior.csv 的工作目录

见下文,其中一些文件的编号与 111868-Behavior.csv111868-Behavior 2.csv 相同,Locations.csv 也是如此

#here some of the csv in the work directory

dir()
  [1] "111868-Behavior 2.csv"            "111868-Behavior.csv"             
  [3] "111868-Locations 2.csv"           "111868-Locations.csv"            
  [5] "111869-Behavior.csv"              "111869-Locations.csv"            
  [7] "111870-Behavior 2.csv"            "111870-Behavior.csv"             
  [9] "111870-Locations 2.csv"           "111870-Locations.csv"            
 [11] "112696-Behavior 2.csv"            "112696-Behavior.csv"             
 [13] "112696-Locations 2.csv"           "112696-Locations.csv"    

我无法更改文件名。

我想导入所有 36 个位置和 36 个行为,但是当我尝试这样做时

#Create list of all behaviors
bhv <- list.files(pattern="*-Behavior.csv")
bhv2 <- list.files(pattern="*-Behavior 2.csv")

#Throw them altogether
bhv_csv = ldply(bhv, read_csv)
bhv_csv2 = ldply(bhv2, read_csv)

#Join bhv_csv and bhv_csv2
b<-rbind(bhv_csv,bhv_csv2)

#Create list of all locations
loc <- list.files(pattern="*-Locations.csv")
loc2 <- list.files(pattern="*-Locations 2.csv")

#Throw them altogether
loc_csv = ldply(loc, read_csv)
loc_csv2 = ldply(loc2, read_csv)

#Join loc_csv and loc_csv2
l<-rbind(loc_csv,loc_csv2)

只显示 28 个,而不是我想象的 36 个

length(unique(b$Ptt))
[1] 28

length(unique(l$Ptt))
[1] 28

这个数字28,大约是所有Behaviors.csvLocations.csv,没有Behaviors 2.csvLocations 2.csv(数字为“2”的总共8个)

我想以显示 36 个行为和位置的方式导入所有文件行为和所有位置。我该怎么做?

【问题讨论】:

  • 为什么Behavior 需要两个单独的list.files?你可以list.files(pattern="Behavior") 吗?它应该涵盖两种模式,然后对于 Locations 相同?
  • 我现在做了这个,在列表中创建了 36 个节目,但是当我运行 length(unique(b$Ptt))shows me only 28...
  • 我不知道Ptt 列有什么,但因为您想要两个不同的数据框用于“行为”和“模式”。尝试这样做,b &lt;- rbindlist(lapply(list.files(pattern="Behavior", full.names = TRUE),fread)) 并确保已加载 data.table,因为 freadrbindlist 来自 data.table
  • Ptt 列显示每个行为或位置的编号,例如111868111869(每个数字是一种动物111868-Behavior 2.csv111868-Behavior.csv 是2 种动物,例如),是 36 个数字,但只显示 26。我试过你告诉我的这段代码,但结果是一样的

标签: r csv import filenames


【解决方案1】:

您可以使用purrr::map 来简化您的一些代码:

library("tidyverse")
library("readr")

# Create two small csv files
write_lines("a,b\n1,2\n3,4", "file1.csv")
write_lines("a,c\n5,6\n7,8", "file2.csv")

list.files(pattern = "*.csv") %>%
  # `map` will cycle through the files and read each one
  map(read_csv) %>%
  # and then we can bind them all together
  bind_rows()
#> Parsed with column specification:
#> cols(
#>   a = col_double(),
#>   b = col_double()
#> )
#> Parsed with column specification:
#> cols(
#>   a = col_double(),
#>   c = col_double()
#> )
#> # A tibble: 4 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     2    NA
#> 2     3     4    NA
#> 3     5    NA     6
#> 4     7    NA     8

reprex package (v0.2.1) 于 2019 年 3 月 28 日创建

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2017-03-31
    • 2021-07-23
    相关资源
    最近更新 更多