indexing - R: row index list from list of column values? -


in r, how index list of values 1 r column based on specified list of values column?

i know how select , modify specific row numbers, e.g.:

> foo=data.frame(a=100*1:5,b=letters[5:1]) > foo     b 1 100 e 2 200 d 3 300 c 4 400 b 5 500 > foo$a[c(1,3,5)]= foo$a[c(1,3,5)] + c(3,2,1) > foo     b 1 103 e 2 200 d 3 302 c 4 400 b 5 501 

but if instead want select , modify rows 'b' values "a", "e", , "c"? thought which might correct tool, best i've been able come is:

> foo$a[which(is.element(foo$b,c("a","e","c")))] [1] 103 302 501 

at point i'm stuck, because while i've selected correct rows, aren't in right order can't modify them individually.

removing is.element , use %in% solution if don't care matching order

[edit] - can remove which

foo$a[foo$b %in% c("a","e", "c")] 

another possibility use match provide indices of each element.

foo$a[match(c("a","e","c"),foo$b)] [1] 500 100 300 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -