read.table
Issue when importing dataset: `Error in scan(…): line 1 did not have 145 elements`
This error is pretty self-explanatory. There seem to be data missing in the first line of your data file (or second line, as the case may be since you’re using header = TRUE). Here’s a mini example: ## Create a small dataset to play with cat(“V1 V2\nFirst 1 2\nSecond 2\nThird 3 8\n”, file=”test.txt”) R automatically … Read more
read.csv warning ‘EOF within quoted string’ prevents complete reading of file
You need to disable quoting. cit <- read.csv(“citations.CSV”, quote = “”, row.names = NULL, stringsAsFactors = FALSE) str(cit) ## ‘data.frame’: 112543 obs. of 13 variables: ## $ row.names : chr “10.2307/675394” “10.2307/30007362” “10.2307/4254931” “10.2307/20537934” … ## $ id : chr “10.2307/675394\t” “10.2307/30007362\t” “10.2307/4254931\t” “10.2307/20537934\t” … ## $ doi : chr “Archaeological Inference and Inductive Confirmation\t” … Read more
How can you read a CSV file in R with different number of columns
Deep in the ?read.table documentation there is the following: The number of data columns is determined by looking at the first five lines of input (or the whole file if it has less than five lines), or from the length of col.names if it is specified and is longer. This could conceivably be wrong if … Read more
Specify custom Date format for colClasses argument in read.table/read.csv
You can write your own function that accepts a string and converts it to a Date using the format you want, then use the setAs to set it as an as method. Then you can use your function as part of the colClasses. Try: setAs(“character”,”myDate”, function(from) as.Date(from, format=”%d/%m/%Y”) ) tmp <- c(“1, 15/08/2008”, “2, 23/05/2010”) … Read more
How do you read multiple .txt files into R? [duplicate]
You can try this: filelist = list.files(pattern = “.*.txt”) #assuming tab separated values with a header datalist = lapply(filelist, function(x)read.table(x, header=T)) #assuming the same header/columns for all files datafr = do.call(“rbind”, datalist)