Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

r - Extract second element from every item in a list

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...