Reputation: 131
I would like to combine several of my files into one file. I found the codes from one of the post in the website and adapted it to my own use as followed:
library(dplyr)
library(readr)
df2 <- list.files(pattern = "test_.*\\.csv",full.names = TRUE) %>%
lapply(read_csv) %>%
bind_rows %>%
transmute(Isolate=Iso,
Condit=Condit,
rep=rep,
set=set,
hpi=hpi,
OD=OD)
But I get this error:
Error in mutate_impl(.data, dots) : binding not found: 'Iso'
I don't understand why. Could somebody help me?
Thanks a lot!
The input file 1: test_.1.csv
Iso rep set Condit hpi OD
1 A22.1 1 3 T27 84 0.232
2 A22.1 2 3 T27 84 0.23
3 A22.1 3 3 T27 84 0.214
4 D2.1 1 3 T27 84 0.049
5 D2.1 2 3 T27 84 0.054
6 D2.1 3 3 T27 84 0.049
The input file 2: test_.2.csv
Iso rep set Condit hpi OD
1 A22.1 1 3 T27 72 0.191
2 A22.1 2 3 T27 72 0.186
3 A22.1 3 3 T27 72 0.179
4 D2.1 1 3 T27 72 0.048
5 D2.1 2 3 T27 72 0.053
Upvotes: 3
Views: 10655
Reputation: 42544
I've run your code and got the same error message
Error: binding not found: 'Iso'
Reason for this seems to be read_csv
from the readr
package. When used to read a single file,
read_csv("test_.1.csv")
returns:
Parsed with column specification:
cols(
`Iso rep set Condit hpi OD` = col_character()
)
# A tibble: 6 × 1
`Iso rep set Condit hpi OD`
<chr>
1 A22.1 1 3 T27 84 0.232
2 A22.1 2 3 T27 84 0.23
3 A22.1 3 3 T27 84 0.214
4 D2.1 1 3 T27 84 0.049
5 D2.1 2 3 T27 84 0.054
6 D2.1 3 3 T27 84 0.049
So, read_csv
apparently doesn't know how to split the lines into columns.
The code below using fread()
and rbindlist()
from the data.table
package works for me. It changes the name of the Iso
column as requested by the OP. In addition, it adds a column indicating the origin of each row.
file_names <- list.files(pattern = "test_.*\\.csv", full.names = TRUE)
library(data.table)
df2 <-
rbindlist(
lapply(file_names, fread), idcol = "file_id"
)[, file_id := basename(file_names)[file_id] # add origin
][, setnames(.SD, "Iso", "Isolate")] # rename one column
df2
# file_id Isolate rep set Condit hpi OD
# 1: test_.1.csv A22.1 1 3 T27 84 0.232
# 2: test_.1.csv A22.1 2 3 T27 84 0.230
# 3: test_.1.csv A22.1 3 3 T27 84 0.214
# 4: test_.1.csv D2.1 1 3 T27 84 0.049
# 5: test_.1.csv D2.1 2 3 T27 84 0.054
# 6: test_.1.csv D2.1 3 3 T27 84 0.049
# 7: test_.2.csv A22.1 1 3 T27 72 0.191
# 8: test_.2.csv A22.1 2 3 T27 72 0.186
# 9: test_.2.csv A22.1 3 3 T27 72 0.179
#10: test_.2.csv D2.1 1 3 T27 72 0.048
#11: test_.2.csv D2.1 2 3 T27 72 0.053
Upvotes: 2