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
208 views
in Technique[技术] by (71.8m points)

List comprehension in R

Is there a way to implement list comprehension in R?

Like python:

sum([x for x in range(1000) if x % 3== 0 or x % 5== 0])

same in Haskell:

sum [x| x<-[1..1000-1], x`mod` 3 ==0 || x `mod` 5 ==0 ]

What's the practical way to apply this in R?

Nick

question from:https://stackoverflow.com/questions/15998716/list-comprehension-in-r

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

1 Answer

0 votes
by (71.8m points)

Something like this?

l <- 1:1000
sum(l[l %% 3 == 0 | l %% 5 == 0])

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

...