[R] indexing in data frames

jimi adams jadams at american.edu
Thu Aug 9 22:42:33 CEST 2012


I'm still not fully understanding exactly how R is handling data frames, but am getting closer. Any help with this one will likely go a long way in getting me there. Let's say I have a data frame, let's call it "a". Within that data frame i have two variables, let's call them "b" and "c", where "b" is a single numeric value per observation, while "c" is a LIST of numeric values. What I want to be able to do is perform an operation on each element in "c" by the single element in "b". 

So, for example, if I wanted to subtract each element in "c" from the scalar in "b". For example, if i had

> a$b
[1] 1988
[2] 1989
…
& 
> a$c
[[1]]
[1] 1985 1982 1984
[[2]]
[1] 1988 1980
…

I'm looking for a result of:
a$new
[[1]]
[1] 3 6 4
[[2]]
[1] 1 9
…

I've tried a few different things, none of which have the desired result. Any help appreciated.
thanks!

jimi adams
Assistant Professor
Department of Sociology
American University
e: jadams at american.edu
w: jimiadams.com



More information about the R-help mailing list