簡體   English   中英

如何從 R 中的數據框中刪除指定行,但根據另一個列變量消除行?

[英]How do I remove specified rows from a data frame in R, but the rows are eliminated according to another column variable?

我有一個值數據框,變量名稱對應於每次模擬的坐標和事件時間。

head_data<-structure(list(x = c(987.353265152362, 570.817987386894, 1147.5681499552, 
637.526076016409, 1439.13510253106, 1396.6452808061), y = c(1802.08232812874, 
349.336242713164, 1789.49467712533, 361.611973188148, 1492.44148360367, 
1459.91771610835), id = 1:6, `simulation 1` = c(1100, 600, 1200, 
400, 900, 1000), `simulation 2` = c(1500, 1400, 1600, 1200, 1200, 
1300), `simulation 3` = c(1200, 1100, 1200, 1000, 900, 900), 
    `simulation 4` = c(1300, 800, 1200, 900, 1100, 1100), `simulation 5` = c(1500, 
    1200, 1400, 1100, 1300, 1200), `simulation 6` = c(200, 1400, 
    100, 1100, 600, 600)), row.names = c(NA, 6L), class = "data.frame")

我已經使用 melt 重新排列了這些數據,並從 reshape2 和 dplyr 包中排列。

data_long <- melt(head_data, id.vars = c('x', 'y', 'id'), value.name = 'time', variable.name = 'sim')
data_long_sort<-data_long%>%arrange(sim,time)

每個模擬有 6 個時間值,我想做的是消除每個模擬中的 3 個最高值,所以我有一個看起來像這樣的表

data_trim<-structure(list(x = c(637.526076016409, 570.817987386894, 1439.13510253106, 
637.526076016409, 1439.13510253106, 1396.6452808061, 1439.13510253106, 
1396.6452808061, 637.526076016409, 570.817987386894, 637.526076016409, 
1439.13510253106, 637.526076016409, 570.817987386894, 1396.6452808061, 
1147.5681499552, 987.353265152362, 1439.13510253106), y = c(361.611973188148, 
349.336242713164, 1492.44148360367, 361.611973188148, 1492.44148360367, 
1459.91771610835, 1492.44148360367, 1459.91771610835, 361.611973188148, 
349.336242713164, 361.611973188148, 1492.44148360367, 361.611973188148, 
349.336242713164, 1459.91771610835, 1789.49467712533, 1802.08232812874, 
1492.44148360367), id = c(4L, 2L, 5L, 4L, 5L, 6L, 5L, 6L, 4L, 
2L, 4L, 5L, 4L, 2L, 6L, 3L, 1L, 5L), sim = structure(c(1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L
), .Label = c("simulation 1", "simulation 2", "simulation 3", 
"simulation 4", "simulation 5", "simulation 6"), class = "factor"), 
    time = c(400, 600, 900, 1200, 1200, 1300, 900, 900, 1000, 
    800, 900, 1100, 1100, 1200, 1200, 100, 200, 600)), row.names = c(1L, 
2L, 3L, 7L, 8L, 9L, 13L, 14L, 15L, 19L, 20L, 21L, 25L, 26L, 27L, 
31L, 32L, 33L), class = "data.frame")

我這樣做了

data_trim<-data_long_sort[c(1:3,7:9,13:15,19:21,25:27,31:33),]

但是對於更大的數據框,我需要一種更有效的方法。

這是使用 tidyr 和 dplyr package 的簡明答案:

library(tidyr)
library(dplyr)

data_long_sort <- head_data %>% 
                  pivot_longer(cols=starts_with("sim"), names_to="sim", values_to="time") %>% 
                  arrange(sim,time)

answer <-data_long_sort %>% group_by(sim) %>% slice_head(n=3)

#a more general option with a variable number of simulation columns
data_long_sort %>% group_by(sim) %>% slice_head(n= nrow(.)-3)

我們可以使用filter

library(dplyr)
data_long_sort  %>%
   group_by(sim) %>% 
   filter(row_number() <=3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM