![](/img/trans.png)
[英]Importing and combining multiple CSV files in R with differing numbers and names of rows
[英]Importing multiple .csv files into R with differents names
我有一個工作目錄,包含37個Locations.csv和37個Behavior.csv
看下面有一些文件與111868-Behavior.csv
和111868-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.csv
和Locations.csv
沒有Behaviors 2.csv
和Locations 2.csv
(數字為“2”的那些每個都是8)
我想以一種顯示36個行為和位置的方式導入所有文件行為和所有位置。 我怎樣才能做到這一點?
您可以使用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包創建於2019-03-28(v0.2.1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.