0

I have a set of values in text file-

34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
45.08327747668339,56.3163717815305,0

Now I am importing these values in R-

mydata = read.table("D:/tmp/mlclass-ex1-005/mlclass-ex2-005/R-Studio/ex2data1.txt",header=TRUE,sep=",");

But while importing I want to format the values with only 2 values after decimal means

34.62365962451697,78.0246928153624,0

will be loaded as

34.62,78.02,0

I know I should run "format" to do the needful but how can I achieve the same while importing itself. or what is the other way out to format mydata because if I try to run

format(round(mydata,2),nsmall=2)

it throws the error-

Error in Math.data.frame(list(test1 = c(9L, 2L, 11L, 38L, 73L, 19L, 41L,  : 
  non-numeric variable in data frame: test1test2

Structure of my data after reading

  str(mydata)
'data.frame':   100 obs. of  3 variables:
 $ test1   : Factor w/ 100 levels "30.05882244669796",..: 9 2 11 38 73 19 41 65 68 83 ...
 $ test2   : Factor w/ 100 levels "30.60326323428011",..: 72 15 62 80 66 36 95 21 83 14 ...
 $ admitted: int  0 0 0 1 1 0 1 1 1 1 ...
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128

2 Answers2

1

If your data.frame isn't all numbers, you can use something like this:

mydata[] <- lapply(mydata, function(x) {if (is.numeric(x)) round(x, 2) else x})
Roland
  • 127,288
  • 10
  • 191
  • 288
  • this is not making any change to my dataset – Abhishek Choudhary May 14 '14 at 12:14
  • 1
    @Roland, Perhaps a case of [**this**](http://stackoverflow.com/questions/23634964/why-is-r-reading-numeric-data-as-character/23635217#23635217). I asked OP in a previous comment to `dput` data. – Henrik May 14 '14 at 12:14
  • @ABC look at `str(mydata)`. It seems like your data isn't read in as numbers. Henrik points out a possible reason another are strings somewhere in the file (e.g., NA strings). – Roland May 14 '14 at 12:16
0

Maybe I misunderstand the question, but it seems to me that

mydata = read.table("D:/tmp/mlclass-ex1-005/mlclass-ex2-005/R-Studio/ex2data1.txt",header=TRUE,sep=",");
mydata=round(mydata,2)

should do the trick ?

jfmoyen
  • 495
  • 2
  • 11
  • `round` expects a vector. Your code is passing it a data.frame. – Roland May 14 '14 at 11:51
  • 2
    @Roland, although not described in the help text, `round` seems to happily eat data frames: `df <- data.frame(x = rnorm(5), y = rnorm(5))`; `round(df)`. Perhaps worth posting on R-devel (is that the appropriate mailing list?)? – Henrik May 14 '14 at 11:56
  • @Henrik Thanks. I didn't know that. But after reading it I'm not sure that this isn't actually in the documentation. I don't know much about the group generics. – Roland May 14 '14 at 12:05
  • @Roland: I tried `round()` (on a matrix, actually) without reading the doc first ... and it worked ! Only after reading your comment did I go back to the man and noticed that `round()` is indeed supposed to take a vector as an argument, not a data frame... still I can confirm that something like `foo<-matrix(runif(25),5); round(foo,2)` works happily. – jfmoyen May 14 '14 at 12:06
  • round in my case throws the error i posted Error in Math.data.frame(list(test1 = c(9L, 2L, 11L, 38L, 73L, 19L, 41L, : non-numeric variable in data frame: test1test2 – Abhishek Choudhary May 14 '14 at 12:07
  • @Roland, Me neither (didn't know that & don't know much about...). – Henrik May 14 '14 at 12:07
  • @ABC, Please `dput` your data. The error message suggests that your data is non-numeric. Possibly due to [**this**](http://stackoverflow.com/questions/23634964/why-is-r-reading-numeric-data-as-character/23635217#23635217) – Henrik May 14 '14 at 12:11
  • @Henrik I am quite a new user to R , so I tried dput but couldn;t change anything , so I ran mydata = data.matrix(mydata) which change even the values of my data ... – Abhishek Choudhary May 14 '14 at 12:24
  • @ABC, I forgot to refer you to [**this post**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) where how to create a reproducible example is described, including the use of `dput`. Cheers. – Henrik May 14 '14 at 12:27