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
How to index an element of a list object in R
Indexing a list is done using double bracket, i.e. hypo_list[[1]] (e.g. have a look here: http://www.r-tutor.com/r-introduction/list). BTW: read.table does not return a table but a dataframe (see value section in ?read.table). So you will have a list of dataframes, rather than a list of table objects. The principal mechanism is identical for tables and dataframes … Read more
How to avoid: read.table truncates numeric values beginning with 0
As said in Ben’s answer, colClasses is the easier way to do it. Here is an example: read.table(text=”col1 col2 0012 0001245″, head=T, colClasses=c(‘character’,’numeric’)) col1 col2 1 0012 1245 ## col1 keep 00 but not col2
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