read.csv
Imported a csv-dataset to R but the values becomes factors
Both the data import function (here: read.csv()) as well as a global option offer you to say stringsAsFactors=FALSE which should fix this.
Combine several data frames in the global environment by row (rbind)
Since you have already read the files in, you can try the following: do.call(rbind, mget(ls(pattern = “df”))) The ls(pattern = df) should capture all of your “df.1”, “df.2”, and so on. Hopefully you don’t have other things named with the same pattern, but if you do, experiment with a stricter pattern until the command lists … Read more
How to detect the right encoding for read.csv?
First of all based on more general question on StackOverflow it is not possible to detect encoding of file in 100% certainty. I’ve struggle this many times and come to non-automatic solution: Use iconvlist to get all possible encodings: codepages <- setNames(iconvlist(), iconvlist()) Then read data using each of them x <- lapply(codepages, function(enc) try(read.table(“encoding.asc”, … Read more
read.csv, header on first line, skip second line [duplicate]
This should do the trick: all_content = readLines(“file.csv”) skip_second = all_content[-2] dat = read.csv(textConnection(skip_second), header = TRUE, stringsAsFactors = FALSE) The first step using readLines reads the entire file into a list, where each item in the list represents a line in the file. Next, you discard the second line using the fact that negative … Read more
How to check if CSV file has a comma or a semicolon as separator?
Here are a few approaches assuming that the only difference among the format of the files is whether the separator is semicolon and the decimal is a comma or the separator is a comma and the decimal is a point. 1) fread As mentioned in the comments fread in data.table package will automatically detect the … Read more
read.csv doesn’t seem to detect factors in R 4.0.0
As Ronak Shah said in a comment to your question, R 4.0.0 changed the default behavior in how read.table() (and so its wrappers including read.csv()) treats character vectors. There has been a long debate over that issue, but basically stringsAsFactors == T setting was a default since the inception of R because it helped to … Read more
Why am I getting X. in my column names when reading a data frame?
read.csv() is a wrapper around the more general read.table() function. That latter function has argument check.names which is documented as: check.names: logical. If ‘TRUE’ then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names. If necessary they are adjusted (by ‘make.names’) so that they … Read more
Specifying colClasses in the read.csv
You can specify the colClasse for only one columns. So in your example you should use: data <- read.csv(‘test.csv’, colClasses=c(“time”=”character”))