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

r - 如何删除数据框中的行?(How do I delete rows in a data frame?)

I have a data frame named "mydata" that looks like this this:

(我有一个名为“ mydata”的数据框,看起来像这样:)

   A  B  C   D 
1. 5  4  4   4 
2. 5  4  4   4 
3. 5  4  4   4 
4. 5  4  4   4 
5. 5  4  4   4 
6. 5  4  4   4 
7. 5  4  4   4 

I'd like to delete row 2,4,6.

(我想删除第2、4、6行。)

For example, like this:

(例如,像这样:)

   A  B  C   D
1. 5  4  4  4 
3. 5  4  4  4 
5. 5  4  4  4 
7. 5  4  4  4 
  ask by R newbie translate from so

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

1 Reply

0 votes
by (71.8m points)

The key idea is you form a set of the rows you want to remove, and keep the complement of that set.

(关键思想是形成要删除的行的集合,并保留该集合的补充。)

In R, the complement of a set is given by the '-' operator.

(在R中,集合的补码由'-'运算符给出。)

So, assuming the data.frame is called myData :

(因此,假设data.frame称为myData :)

myData[-c(2, 4, 6), ]   # notice the -

Of course, don't forget to "reassign" myData if you wanted to drop those rows entirely---otherwise, R just prints the results.

(当然,如果您想完全删除那些行,请不要忘记“重新分配” myData否则,R只会打印结果。)

myData <- myData[-c(2, 4, 6), ]

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

...