0

I have a data frame that looks something like this

dput(head(data.frame(head(data, 10))))

structure(list(columns = c(" 873372.97240901832 1027036.2826745831 -70115.084934771265", 
" 965481.29235509655 939652.10830702272 -86022.028960607044", 
" 1064430.7931216138 823466.6941767697 -106722.49170664093", 
" 1157212.3898099209 684973.42778737482 -119041.53937019428", 
" 1231967.9138633562 540785.35503704555 -110933.57917522889", 
" 1281645.9293598456 415675.65857948799 -84246.416065295387")), .Names = "columns", row.names = c(NA, 
6L), class = "data.frame")

                                                     columns
1  873372.97240901832 1027036.2826745831 -70115.084934771265
2  965481.29235509655 939652.10830702272 -86022.028960607044
3   1064430.7931216138 823466.6941767697 -106722.49170664093
4  1157212.3898099209 684973.42778737482 -119041.53937019428
5  1231967.9138633562 540785.35503704555 -110933.57917522889
6  1281645.9293598456 415675.65857948799 -84246.416065295387

How do I get these 3 values in 3 different columns? I have been using strspilt, but it is rounding my values. I need to maintain precision. Unfortunately, I am unable to spilt these in the reading in of the file.

jbaums
  • 27,115
  • 5
  • 79
  • 119
Mike.Gahan
  • 4,565
  • 23
  • 39
  • Why are you unable to do it when reading the file? Also, `strsplit` shouldn't be doing any rounding, since the data are character strings, not numeric. – jbaums May 04 '14 at 22:49
  • It is an omf file. Unfortunately, this is the only way I have figured out how to read it in. – Mike.Gahan May 04 '14 at 22:51
  • R doesn't show all the decimal place in the GUI by default. You can dput() the split results (which i guess you're doing something like this dput(lapply(strsplit(data$columns," "), as.numeric)) to see what's really there. You can set options(digit=) to something higher if you like. And why do you need that much precision anyway? – MrFlick May 04 '14 at 22:53
  • Are .omf tabular text files? Have you tried `read.table('my.omf')`? – jbaums May 04 '14 at 22:55
  • Here is the file: http://www.sendspace.com/file/lsnf9c. I have been using `data <- fread(x,skip=37,sep="\n")` to read in the file. Obviously, I would want to use `sep=" "`, but that is erroring out. – Mike.Gahan May 04 '14 at 22:58
  • 1
    Try `read.table('~/../Downloads/my_step-Oxs_TimeDriver-Magnetization-00-0000042.omf', skip=grep('# Begin: Data Text', readLines('~/../Downloads/my_step-Oxs_TimeDriver-Magnetization-00-0000042.omf')))`. Seems to work fine for me. (You need to skip 38, I believe.) – jbaums May 04 '14 at 23:30
  • Works great. I wish I could get it to work with `fread` since I have a lot of files to loop through and I want it to be as fast as possible. But I might just have to settle for `read.table`. Very much appreciated. – Mike.Gahan May 04 '14 at 23:34
  • 1
    The problem for `fread` is the leading space, I think (mentioned [here](http://stackoverflow.com/questions/22344161/fread-unable-to-read-csv-files-with-first-column-empty)) – jbaums May 04 '14 at 23:59
  • Well, you've got other issues with that data file. There are lines with triples of positive and negative "0"'s (e.g. `0 -0 -0`). This rly sounds like a job for (starters) `grep -v "^\#"` on the command line (install `grep` for Windows if you're stuck there). How do you plan on handling those very different lines? – hrbrmstr May 05 '14 at 00:51
  • @hrbrmstr `grep` is available in R, and can be used with `readLines` to find the start of the data, as indicated in my previous comment. And `-0` resolves to 0 so shouldn't be a problem as long as that's the intended behaviour. – jbaums May 05 '14 at 03:53

0 Answers0