Reputation: 43
This additonal information might help. Here is what I am trying to do: This throws more light but here is what I am trying to. Lets say you have a data like the one below –
Region Open Store
120..141 + France
145..2115 + Germany
3322..5643 + Wales
5646..7451 - Scotland
7454..8641 - Mexico
8655..9860 - India
9980..11413 + Zambia
11478..1261 - Nicaragua
12978..1318 + Sweeden
What I was trying to do was to pick find the difference between the second element (141) and the consecutive first element (143) and if they meet a certain value and they have the same sign ( + or -), list all their stores together.
Upvotes: 3
Views: 7653
Reputation: 78832
I agree with @Dason that understanding the mechanics are important (i.e. you can't treat R like PHP), but if 2.6 hours of fiddling still has you stumped, this might help:
dat <- list(c(100, 150), c(201, 202), c(147, 269), c(301, 401))
second <- sapply(dat, "[", 2)
first <- sapply(dat[2:length(second)], "[", 1)
second[1:length(first)] - first
Knowing more of what you're really trying to do would help since I'm not fully convinced that's the final result you're looking for.
Upvotes: 3
Reputation: 61953
Use sapply
and pass to it [
as the function and tell it you want to extract the second element.
# Create your data set
dat <- list(c(100, 150), c(201, 202), c(147, 269), c(301, 401))
dat
#[[1]]
#[1] 100 150
#
#[[2]]
#[1] 201 202
#
#[[3]]
#[1] 147 269
#
#[[4]]
#[1] 301 401
#
sapply(dat, "[", 2)
#[1] 150 202 269 401
Upvotes: 10