[R] help with data.frame

arun smartpink111 at yahoo.com
Tue May 21 21:02:21 CEST 2013


Hi,
library(stringr)
b[str_detect(colnames(b),"^y")]
 #        y       y.1       y.2
#1  0.00000  0.000000  0.000000
#2 19.55811 17.023812 15.354880
#3 10.74991  9.024250  8.177128
#4  5.91924  4.789331  4.367188


#or
b[,!is.na(match(gsub("\\..*","",names(b)),"y"))]
#         y       y.1       y.2
#1  0.00000  0.000000  0.000000
#2 19.55811 17.023812 15.354880
#3 10.74991  9.024250  8.177128
#4  5.91924  4.789331  4.367188
A.K.




Dear All 

I have the following code for list "a": 

a <-list(structure(c(0, 4, 8, 12, 0, 19.5581076131386, 10.7499105081144, 
5.91923975728553, 0, 4.08916328337685, 2.26872955281708, 1.24929641535359 
), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("time", "y", "b" 
)), istate = c(2L, 107L, 250L, NA, 5L, 5L, 0L, 52L, 22L, NA, 
NA, NA, NA, 0L, 1L, 1L, NA, NA, NA, NA, NA), rstate = c(0.867511261090201, 
0.867511261090201, 12.7772879103809, 0, 0), lengthvar = 2L, class = c("deSolve", 
"matrix"), type = "lsoda"), structure(c(0, 4, 8, 12, 0, 17.0238115689622, 
9.02425032330714, 4.7893314106951, 0, 4.45067278743554, 2.37140075611636, 
1.25855947034654), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("time", 
"y", "b")), istate = c(2L, 106L, 251L, NA, 4L, 4L, 0L, 52L, 22L, 
NA, NA, NA, NA, 0L, 1L, 1L, NA, NA, NA, NA, NA), rstate = c(0.662055762167652, 
0.662055762167652, 12.3096826617166, 0, 0), lengthvar = 2L, class = c("deSolve", 
"matrix"), type = "lsoda"), structure(c(0, 4, 8, 12, 0, 15.3548797334796, 
8.17712839316703, 4.36718847853436, 0, 5.15624657530424, 2.77411694866808, 
1.48166036763212), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("time", 
"y", "b")), istate = c(2L, 108L, 260L, NA, 5L, 5L, 0L, 52L, 22L, 
NA, NA, NA, NA, 0L, 1L, 1L, NA, NA, NA, NA, NA), rstate = c(0.735884123193699, 
0.735884123193699, 12.1878866053931, 0, 0), lengthvar = 2L, class = c("deSolve", 
"matrix"), type = "lsoda")) 

then I convert it to "b" 

b <-data.frame(a) 

and manually I would extract the y variables (y, y.1 and y.2) as follows 

d <-t(cbind(b$y,b$y.1,b$y.2)) 

Currently I only have 3 y variables, so manual solution is very 
easy. I would like to ask if you have any thoughts on how I could 
"automate" (or extract all ys) this so that I could achieve the same 
goal even if I have 5000 ys (from y, y.1, y.2.... to y.5000) or any 
other number of ys for that matter with a simple code (as opposed to 
something like d <-t(cbind(b$y,b$y.1,b$y.2,....b$y.5000))). 

your help is greatly appreciated, 

thanks, 

Andras



More information about the R-help mailing list