Monday, October 30, 2017
Sweet and short R set-diff code
Was working on a project in R and was using extensively with the 'names' property of a vector. Basically, I use the vector to form the menu for user to pick off the desired choice. To provide user enough information, lines and lines information is embedded in the names property.
All of these was working well until I let user to pick items to be removed from the list/vector repeatedly.
As is a reasonable approach, all the program need to do is to use R's set operation: setdiff() to remove picked items from the original vector/list. Unfortunately, the setdiff() function removes all names from the list.
Here's my solution: v1 <- v1[!is.element(v1,Picked_items)];
In this way, all names are retained.
Don't you just love R - no loop is needed.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment