Skip to content
Prev 302928 / 398503 Next

indexing in data frames

On Aug 9, 2012, at 2:43 PM, David L Carlson wrote:

            
Not true:

 > dput(a)
structure(list(b = c(1988, 1989),
                c = list(c(1985, 1982, 1984),
                         c(1988, 1980))), .Names = c("b", "c"))

 > ab <- data.frame(a$b)
 > ab
    a.b
1 1988
2 1989
 > ab$cb <- a$c
 > ab
    a.b               cb
1 1988 1985, 1982, 1984
2 1989       1988, 1980
 > str(ab)
'data.frame':	2 obs. of  2 variables:
  $ a.b: num  1988 1989
  $ cb :List of 2
   ..$ : num  1985 1982 1984
   ..$ : num  1988 1980

But it seems unlikely that the OP's "a" object is a dataframe since  
the console eval-print loop would not display a dataframe in that  
manner.

At any rate with the ab dataframe:

 > for( i in 1:NROW(ab) ) print(  ab$a.b[i] - ab$cb[[i]] )
[1] 3 6 4
[1] 1 9

The OP should note the need to use '[[' on a list-object to get  
commensurate classes to pass to the '-' operator.