Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

dplyr - Importing multiple .csv files with variable column types into R

How can I properly build an lapply to read (from out of one directory) all the .csv files, load all the columns as strings and then bind them into one data frame.

Per this, I have a way to get all the .csv files loaded and bound into a dataframe. Unfortunately they are getting hung up on the variablity of how the columns are getting type cast. Thus giving me this error:

Error: Can not automatically convert from character to integer in column

I have tried supplementing the code with the arguments for data type and am trying to just keep everything as characters; I am getting stuck now on being able to properly get my lapply 'loop' to effectively reference the subject of each cycle of its 'loop'.

srvy1 <- structure(list(RESPONSE_ID = 584580L, QUESTION_ID = 328L, SURVEY_ID = 2324L, 
           AFF_ID_INV_RESP = 5L), .Names = c("RESPONSE_ID", "QUESTION_ID", 
                                             "SURVEY_ID", "AFF_ID_INV_RESP"), class = "data.frame", row.names = c(NA, 
                                                                                                                  -1L))

srvy2 <- structure(list(RESPONSE_ID = 584580L, QUESTION_ID = 328L, SURVEY_ID = 2324L, 
           AFF_ID_INV_RESP = "bovine"), .Names = c("RESPONSE_ID", "QUESTION_ID", 
                                                   "SURVEY_ID", "AFF_ID_INV_RESP"), class = "data.frame", row.names = c(NA, 
                                                                                                                        -1L))    

files = list.files(pattern="*.csv")
tbl = lapply(files, read_csv(files, col_types = cols(.default = col_character()))) %>% bind_rows

Is there an easy fix for this that I can keep in tidyverse, or must I drop down a level and go into openly building the for loop myself - per this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The lapply should be the form lapply(x, FUN, ...) where ... is the arguments passed to FUN. You're filling the arguments within FUN. It should be lapply(files, read_csv, col_types = cols(.default = "c"))

If you like a tidyverse solution:

files %>%
  map_df(~read_csv(.x, col_types = cols(.default = "c")))

Which will bind the whole thing into a data frame at the end.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...