[R] extract data from a data.frame

Kort, Eric Eric.Kort at vai.org
Tue Jan 13 01:58:43 CET 2004


> -----Original Message-----
> From: Ian Garcia [mailto:tazapa at yahoo.com]
>  Hi,
> 
>  I'm reading part of a table from postgres, so I'm
> getting a data frame. 
> 
>  how can I extract the numerica values so I can 
>  operate on them.
> 
> > res <- dbGetResult(mydata)
> > str(res)
> `data.frame':   5 obs. of  8 variables:
>  $ cyx.1: num   0.149 -0.278  0.114  0.060  0.109
>  $ cyx.2: num   0.158 -0.070  0.063  0.149  0.150
>  $ cyx.3: num   0.052 -0.350  0.114  0.126  0.238
>  $ cyx.4: num   0.085 -0.009  0.097  0.121  0.258
>  $ cyx.5: num   0.131 -0.028  0.068 -0.106  0.029
>  $ cyx.6: num   0.140 -0.090  0.242 -0.071  0.713
>  $ cyx.7: num   0.741 -0.290  0.215 -0.556  0.979
>  $ cyx.8: num   0.129  0.043  0.287  0.1945 0.0265

#one way
res$cyx.1

#as in:
sum(res$cyx.1)

#another way:
attach res
sum(cyx.1)

#or by row index
res[1, ]

#as in
for (i in 1:nrow(res)) {
  print(sum(res[i,]))
}

#or by column index
for (i in 1:ncol(res)) {
  print(sum(res[,i]))
}

hope that helps.
-Eric

> 
>  Thanks
> 
>  Ian
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
This email message, including any attachments, is for the so...{{dropped}}




More information about the R-help mailing list