[R] Odp: fail to recover decimal value from data frame

Petr PIKAL petr.pikal at precheza.cz
Wed Sep 12 16:50:50 CEST 2007


Hi

you definitely shall try to get acustommed with data structures in R. You 
do ***not*** have numeric decimal values but factors (probably created by 
transfering data to R as you have in the column a value psi1).  Then the 
assignement places a factor code into matrix instead of a factor value. 

fac<-factor(rnorm(100))
daf<-as.data.frame(matrix(fac,10,10))
> sapply(daf, "is.factor")
  V1   V2   V3   V4   V5   V6   V7   V8   V9  V10 
TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 

mat[1,10]<-daf[1,10]
> mat
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA     1
 [2,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [3,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA

<snip>

> mat[2,10]<-daf[2,10]
mat[2,10]
[1] 4
daf.n<-sapply(daf, function(x) as.numeric(as.character(x)))
mat[3,10]<-daf.n[3,10]
mat
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]     [,10]
 [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA 1.0000000
 [2,]   NA   NA   NA   NA   NA   NA   NA   NA   NA 4.0000000
 [3,]   NA   NA   NA   NA   NA   NA   NA   NA   NA 0.2200095
 [4,]   NA   NA   NA   NA   NA   NA   NA   NA   NA        NA
 [5,]   NA   NA   NA   NA   NA   NA   NA   NA   NA        NA
 
You need to get rid of irrelevant values in your data frame first.

Regards

Petr
petr.pikal at precheza.cz

r-help-bounces at r-project.org napsal dne 12.09.2007 16:03:36:

> Hi,
> I have some decimal values stored in a data frame (myframe) and I 
> need to place them in a matrix (mymatrix) using a for loop.
> When I type:
> 
>  > myframe[i,10]
> 
> I get:
> 
> [1] 0.8714
> 434 Levels: 0 0.0134 0.1062 0.1182 0.1241 0.1322 0.1374 0.1429 
> 0.1507 ... psi1
> 
> But when I assign that value to the matrix with:
> 
>  > mymatrix[i,2]<-myframe[i,10]
> 
> The value that gets placed in the matrix is 286, and not 0.8714 as I 
> wanted it to be. What is wrong?
> The data frame does have several rows of character cells interspersed 
> with "NA" and decimal values. It is not very simple I am just using 
> it to store a large output worksheet from where I want to draw 
> selected values.
> 
> Thanks for any help.
> 
> G.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list