read.table

read.table reads “T” as TRUE and “F” as FALSE, how to avoid?

余生长醉 提交于 2019-11-30 17:22:41
I have a file with the data c("A","T","B","F") . When I use: read.csv(myfile,header=F,stringsAsFactors=F) R interprets character T as TRUE and F as FALSE Am I doing anything wrong? If all your columns are characters then try this: # replace text = . with your filename read.csv(text="A,B,T,T", header=FALSE, stringsAsFactors=FALSE, colClasses = c("character")) Else, you'll have to pass the type of each column in colClasses as: colClasses = c("numeric", "numeric", "character", ...) I came across to similar problem here is the solution: #dummy data df <- read.csv(text=" A,B,T,T,F T,T,F,T,text1 A,T

How to read data with different separators?

这一生的挚爱 提交于 2019-11-30 04:13:09
I have a file looks like: a 1,2,3,5 b 4,5,6,7 c 5,6,7,8 ... That the separator between 1st and 2nd is '\t', other separators are comma. How can I read this kind of data set as as dataframe having 5 fields. I'd probably do this. read.table(text = gsub(",", "\t", readLines("file.txt"))) V1 V2 V3 V4 V5 1 a 1 2 3 5 2 b 4 5 6 7 3 c 5 6 7 8 Unpacking that just a bit: readLines() reads the file into R as a character vector with one element for each line. gsub(",", "\t", ...) replaces every comma with a tab, so that now we've got lines with just one kind of separating character. The text = argument to

preserve old (pre 3.1.0) type.convert behavior

寵の児 提交于 2019-11-30 01:11:46
问题 R 3.1.0 is out and one of the new features is the following: type.convert() (and hence by default read.table() ) returns a character vector or factor when representing a numeric input as a double would lose accuracy. Similarly for complex inputs. To give an example: df <- read.table(text = "num1 num2 1.1 1.1234567890123456 2.2 2.2 3.3 3.3", header = TRUE) sapply(df, class) # num1 num2 # "numeric" "factor" while with previous versions, read.table would have returned two numeric columns. For

Importing many files at the same time and adding ID indicator

早过忘川 提交于 2019-11-29 08:48:36
I have 91 files - .log format: rajectory Log File Rock type: 2 (0: Sphere, 1: Cuboid, 2: Rock) Nr of Trajectories: 91 Trajectory-Mode: ON Average Slope (Degrees): 28.05 / 51.99 / 64.83 Filename: test_tschamut_Pos1.xml Z-offset: 1.32000 Rock Position X: 696621.38 Rock Position Y: 167730.02 Rock Position Z: 1679.6400 Friction: Overall Type: Medium t (s) x (m) y (m) z (m) p0 () p1 () p2 () p3 () vx (m s-1) vy (m s-1) vz (m s-1) wx (rot s-1) wy (rot s-1) wz (rot s-1) Etot (kJ) Ekin (kJ) Ekintrans (kJ) Ekinrot (kJ) zt (m) Fv (kN) Fh (kN) Slippage (m) mu_s (N s m-1) v_res (m s-1) w_res (rot s-1)

How to read a subset of large dataset in R?

。_饼干妹妹 提交于 2019-11-29 06:05:50
I have a dataset with about 2 million rows, so without reading the whole dataset I want to read a subset of dataset . My dataset contains a date column in it so I just want to read dataset between a date range without reading whole dataset as it will be time consuming and memory waste. so how to accomplish it can anyone guide me on this ? Olli J Use skip= parameter in read.table read.table("file.txt",skip= ,nrows= ) Both the skip= and nrows= take in row indicator numbers so just add them after the=. The nrows= defines how deep you range when you are importing the file. I suggest reading https:

automatically detect date columns when reading a file into a data.frame

泪湿孤枕 提交于 2019-11-29 03:50:24
When reading a file, the read.table function uses type.convert to distinguish between logical, integer, numeric, complex, or factor columns and store them accordingly. I'd like to add dates to the mix, so that columns containing dates can automatically be recognized and parsed into Date objects. Only a few date formats should be recognized, e.g. date.formats <- c("%m/%d/%Y", "%Y/%m/%d") Here is an example: fh <- textConnection( "num char date-format1 date-format2 not-all-dates not-same-formats 10 a 1/1/2013 2013/01/01 2013/01/01 1/1/2013 20 b 2/1/2013 2013/02/01 a 2013/02/01 30 c 3/1/2013 NA b

How to read data with different separators?

半城伤御伤魂 提交于 2019-11-29 01:01:34
问题 I have a file looks like: a 1,2,3,5 b 4,5,6,7 c 5,6,7,8 ... That the separator between 1st and 2nd is '\t', other separators are comma. How can I read this kind of data set as as dataframe having 5 fields. 回答1: I'd probably do this. read.table(text = gsub(",", "\t", readLines("file.txt"))) V1 V2 V3 V4 V5 1 a 1 2 3 5 2 b 4 5 6 7 3 c 5 6 7 8 Unpacking that just a bit: readLines() reads the file into R as a character vector with one element for each line. gsub(",", "\t", ...) replaces every

Preventing column-class inference in fread()

允我心安 提交于 2019-11-28 11:58:45
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, "fff"), y=c(1:100, NA,NA,NA)) write.csv(d, "test.csv", row.names=F) in_d <- read.csv("test.csv", nrow=100

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

南楼画角 提交于 2019-11-28 11:56:27
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 243199373 Command: chromosomes <- read.csv(chromFile, sep="\t",skip =0, header = TRUE, ) I want to first

read.table() and read.csv both Error in Rmd

安稳与你 提交于 2019-11-28 07:25:36
I want to read a txt in Rmd --- title: "Untitled" output: html_document --- ```{r} country <- read.table("country.txt") country ``` It show error: processing file: Preview-2878539db5c7.Rmd Quitting from lines 6-8 (Preview-2878539db5c7.Rmd) Error in file(file, "rt") : cannot open the connection Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> read.table - > file Execution halted But I can run code in R console successfully > country <- read.table("country.txt") > country production1 education1 fir1 inflation1 lq1 nonstatein1 patent1 tax1 trade1 2001 52920.47 132649.4