I currently have 19 excel files with 7 columns in 11 of them and 9 columns in the other 8. Both files essentially have the exact some data, but the last 9 just has some extra that I don't need. My ultimate goal is to pluck out the 4 columns that I need to manipulate and have them all as one data.frame so I can perform cross tabulations later.
I have successfully accomplished importing a singular file using the following code to deal with my formatting issues:
import <- read.csv(file = "myfile.csv", head = TRUE, sep = "|", stringAsFactors = FALSE)
I am trying to come up with a succinct way to import this data in one shot. I found this SO Question and tried using the codes provided as there are tons of suggestions, however I only got one option to actually work for me without producing an error or a warning.
temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i], head = TRUE,
sep = "|", stringAsFactors = FALSE))
My problem now, the files I wish to import are displaying in the global environment. They show as data.frame types with accurate lengths, size and values for their respective contents. But I can't call any of them. In the comments of that question, someone asked about this but I did not exactly see any replies to remedy it. Although there are file names displayed (being the original excel document name) these names contain spaces and if I try to call them based on the file names R doesn't like it and won't do it.
As an alternative import method, I also attempted the following which I believe began to work:
# Get the files names
files = list.files(pattern="*.csv")
# First apply read.csv, then rbind
myfiles = do.call(rbind, lapply(files, function(x) read.csv(x, head = TRUE,
sep = "|", stringsAsFactors = FALSE)))
but because the first 11 files have 7 columns and the rest have 9, I was given error message
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match
Maybe separating the files into different directories could aid in this but seeing as I am providing this for others to use, it would be more professional to have a code that does it for them.
So Ultimately: I either need to know how to access this data or if there is an alternative way to do this I would be so greatly appreciative!