read.table

Problems with reading a txt file (EOF within quoted string)

江枫思渺然 提交于 2019-11-28 05:19:53
问题 I am trying to use read.table() to import this TXT file into R (contains informations about meteorological stations provided by the WMO): However, when I try to use tmp <- read.table(file=...,sep=";",header=FALSE) I get this error eof within quoted string warning and only 3514 of the 6702 lines appear in ' tmp '. From a quick look at the text file, I couldn't find any seemingly problematic characters. As suggested in other threads, I also tried quote="" . The EOF warning disappeared, but

Read observations in fixed width files spanning multiple lines in R

只谈情不闲聊 提交于 2019-11-28 01:34:22
I am trying to read a dat file in R. The data is available here . The specification about the dataset is available here . The first part of my problem is solved by using the sep option and providing information about where every column starts and providing the na.strings="*" . However, I do not know how to handle a single observation spanning more than 1 line. In this data set, all the observations span 2 lines. Chinmay Patil You actually need read.fwf for this. Set up some sample data txt <- 'Acura Integra Small 12.9 15.9 18.8 25 31 0 1 4 1.8 140 6300 2890 1 13.2 5 177 102 68 37 26.5 11 2705

Ways to read only select columns from a file into R? (A happy medium between `read.table` and `scan`?) [duplicate]

流过昼夜 提交于 2019-11-27 17:23:48
This question already has an answer here: Only read selected columns 3 answers I have some very big delimited data files and I want to process only certain columns in R without taking the time and memory to create a data.frame for the whole file. The only options I know of are read.table which is very wasteful when I only want a couple of columns or scan which seems too low level for what I want. Is there a better option, either with pure R or perhaps calling out to some other shell script to do the column extraction and then using scan or read.table on it's output? (Which leads to the

Reading a CSV file organized horizontally

邮差的信 提交于 2019-11-27 13:16:43
In R, is there a function like read.csv that reads in files where the headers are on the left (or right) as opposed to the top and the data is organized from left to right? So the data would look like: var1,1,2,3,4,5 Looking at the documentation for read.table and read.csv , nothing seems to pop out. The best option I see using those functions is to use read.table and then construct another table whose columns are the rows of the original data and so forth. Let's say your file is called 'data.csv' and it contains: var1,1,2,3,4,5,6 var2,2.1,3.9,4.6,5.2,6.1 var3,M,F,M,F,M,M Note var1 and var3

How to index an element of a list object in R

…衆ロ難τιáo~ 提交于 2019-11-27 12:26:00
I'm doing the following in order to import some txt tables and keep them as list: # set working directory - the folder where all selection tables are stored hypo_selections<-list.files() # change object name according to each species hypo_list<-lapply(hypo_selections,read.table,sep="\t",header=T) # change object name according to each species I want to access one specific element, let's say hypo_list[1]. Since each element represents a table, how should I procced to access particular cells (rows and columns)? I would like to do something like it: a<-hypo_list[1] a[1,2] But I get the following

Cannot read unicode .csv into R

风格不统一 提交于 2019-11-27 08:52:06
I have a .csv file, which contains the following data: "Ա","Բ" 1,10 2,20 I cannot read it into R so that the column names are displayed like they are in the file. d <- read.csv("./Data/1.csv", fileEncoding="UTF-8") head(d) Produces the following: > d <- read.csv("./Data/1.csv", fileEncoding="UTF-8") Warning messages: 1: In read.table(file = file, header = header, sep = sep, quote = quote, : invalid input found on input connection './Data/1.csv' 2: In read.table(file = file, header = header, sep = sep, quote = quote, : incomplete final line found by readTableHeader on './Data/1.csv' > head(d)

How can I read the header but also skip lines - read.table()?

≡放荡痞女 提交于 2019-11-27 07:49:04
Data.txt: Index;Time; 1;2345; 2;1423; 3;5123; The code: dat <- read.table('data.txt', skip = 1, nrows = 2, header =TRUE, sep =';') The result: X1 X2345 1 2 1423 2 3 5123 I expect the header to be Index and Time, as follows: Index Time 1 2 1423 2 3 5123 How do I do that? Beasterfield I am afraid, that there is no direct way to achieve this. Either you read the entire table and remove afterwards the lines you don't want or you read in the table twice and assign the header later: header <- read.table('data.txt', nrows = 1, header = FALSE, sep =';', stringsAsFactors = FALSE) dat <- read.table(

Preventing column-class inference in fread()

笑着哭i 提交于 2019-11-27 06:39:09
问题 Is there a way for fread to mimic the behaviour of read.table whereby the class of the variable is set by the data that is read in. I have numeric data with a few comments underneath the main data. When i use fread to read in the data, the columns are converted to character. However, by setting the nrow in read.table` i can stop this behaviour. Is this possible in fread. (I would prefer not to alter the raw data or make an amended copy). Thanks An example d <- data.frame(x=c(1:100, NA, NA,

Read observations in fixed width files spanning multiple lines in R

一笑奈何 提交于 2019-11-27 04:48:01
问题 I am trying to read a dat file in R. The data is available here. The specification about the dataset is available here. The first part of my problem is solved by using the sep option and providing information about where every column starts and providing the na.strings="*" . However, I do not know how to handle a single observation spanning more than 1 line. In this data set, all the observations span 2 lines. 回答1: You actually need read.fwf for this. Set up some sample data txt <- 'Acura

Cannot read file with “#” and space using read.table or read.csv in R

亡梦爱人 提交于 2019-11-27 03:56:01
问题 I have a file where the first row is a header. The header can have spaces and the # symbol (there may be other special characters as well). I am trying to read this file using read.csv or read.table but it keeps throwing me errors: undefined columns selected more columns than column names My tab-delimited chromFile file looks like: Chromosome# Chr chr Size UCSC NCBI36/hg18 NCBIBuild36 NCBIBuild37 1 Chr1 chr1 247199719 247249719 247249719 249250621 2 Chr2 chr2 242751149 242951149 242951149