当前位置: 动力学知识库 > 问答 > 编程问答 >

data manipulation - Removing rows from a table in R

问题描述:

I used the following code to scrape a table into R.

player.offense.201702050atl = comments.201702050atl[31] %>% html_text() %>% read_html() %>% html_node("#player_offense") %>% html_table()

Then changed the column labels using:

colnames(player.offense.201702050atl) = c("Player", "Tm", "Cmp.Passing", "Att.Passing", "Yds.Passing", "TD.Passing", "Int.Passing", "Sk.Passing", "Yds.Sk.Passing", "Lng.Passing", "Rate.Passing", "Att.Rushing", "Yds.Rushing", "TD.Rushing", "Lng.Rushing", "Tgt.Receiving", "Rec.Receiving", "Yds.Receiving", "TD.Receiving", "Lng.Receiving", "Fmb.Fumbles", "FL.Fumbles")

Next I need to eliminate rows 1, 11, and 12.

I could use:

player.offense.201702050atl.a = player.offense.201702050atl[2:10, ]

player.offense.201702050atl.b = player.offense.201702050atl[13:20, ]

player.offense.201702050atl.c = rbind(player.offense.201702050atl.a, player.offense.201702050atl.b)

However, I have multiple tables in need of similar manipulations; and, the rows which I intend to eliminate, vary with each one. The criteria for a row I desire eliminated is:

All rows for which the value in column 3 is either "Cmp" or "Passing".

Is there a way to run a function that will parse the table, identify the rows that meet the above criteria, and eliminate them?

网友答案:
df <- data.frame(x = c('a', 'b', 'c'), y = c('ca', 'cb', 'cc'), z=c('da', 'db', 'dc'))
  x  y  z
 1 a ca da
 2 b cb db
 3 c cc dc
df[-union(which(df$y == 'cc'),which(df$y == 'ca')),]
Result:
  x  y  z
2 b cb db
网友答案:

Regarding

I desire eliminated is: All rows for which the value in column 3 is either "Cmp" or "Passing".

df <- data.frame(col1 = 1:3, col2 = c('Cmp', 'Passing', 'other'))
df[!df$col2 %in% c('Cmp', 'Passing'), ]
分享给朋友:
您可能感兴趣的文章:
随机阅读: