我有数据框x:
Button TrackNo NextTime
54 G155 2011-04-29 19:20:04
50 H54 2011-04-29 19:25:41
54 G157 2011-04-29 19:47:58
我需要删除30%(统一)具有button==54
(即)的行。我怎么做的?
我知道如何删除条件,说a <- x[x[,1]==54,]
我知道如何随机删除,比如i <- runif(1,length(x)); a <- x[,i]
但是如何同时做到这一点?
答案 0 :(得分:4)
# generate some fake data (the kind you should provide when asking questions)
mydf <- data.frame(button = sample(1:5, 100, replace = TRUE), var1 = runif(100))
find.button.5 <- mydf$button == 5 # find row numbers where button == 5
perc.30 <- round(sum(find.button.5) * 0.3) # find 30% of button 5
button.5 <- which(find.button.5 == TRUE)
sampled.30 <- sample(button.5, perc.30) # row numbers of 30% of button 5
mydf[-sampled.30, ] # in your final output, include all but the 30%
> nrow(mydf[-sampled.30, ])
[1] 93
请注意,最终输出中缺少sampled.30
的行。