Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

dataframe - How to delete rows from a data.frame, based on an external list, using R?

This might be an easy question but i still need some help for using R.

I have a data.frame (main_data), lets say..

NAMES   AGE     LOC
Jyo     23      Hyd
Abid    27      Kar
Ras     24      Pun
Poo     25      Goa
Sus     28      Kar

I wish to remove a few rows based on a list of names. So lets say I have another list of table as follows:

NAMES_list
Jyo
Ras
Poo

So based on this list, if any of the names match to my above "main_data" table, then I would like to remove the whole row contianing them, so the result should be as follows

NAMES   AGE     LOC
Abid    27      Kar
Sus     28      Kar

Can anyone help me how to achive this using R? Thanks in advance.. :)

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Use %in%:

main_data2 <- main_data[ ! main_data$NAMES %in% NAMES_list, ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...