简体   繁体   中英

Reading in data with different column lengths and spaces in R

I have data that looks something like this:

category 2011 2012 2013 2104
word word word 15,000.11 1,000.15 13,001.50 20,000,001.52
word 2,000.120 400,000.00 57,000.523 402,000,111
word word 4,000.120 455,000.02 57,600.87 403,000,111.18
word 2,056.120 678,000.00 670,000.523 402,009,111.65

It is in a .csv file. I want to read it in so it separates into columns but they are all different lengths so I am not sure how. I know I can separate by spaces but some of the words in the first column have spaces between them.

category         2011      2012         2013         2104
word word word  15,000.11  1,000.15    13,001.50    20,000,001.52
word            2,000.120  400,000.00  57,000.523   402,000,111
word word       4,000.120  455,000.02  57,600.87    403,000,111.18
word            2,056.120  678,000.00  670,000.523  402,009,111.65

I apologize if I am not asking this correctly. Thanks for your help!

We can make the delimiter with sub after reading the dataset with readLines

lines[-1] <- sub("^([A-Za-z ]+)(?=\\s[0-9])", "'\\1'", lines[-1], perl = TRUE)
read.table(textConnection(lines), header = TRUE, check.names = FALSE)
#      category      2011       2012        2013           2104
#1 word word word 15,000.11   1,000.15   13,001.50  20,000,001.52
#2           word 2,000.120 400,000.00  57,000.523    402,000,111
#3      word word 4,000.120 455,000.02   57,600.87 403,000,111.18
#4           word 2,056.120 678,000.00 670,000.523 402,009,111.65

data

lines <- readLines('file.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM