如何删除列中的特定响应?

我有一个数据集,其中的列之一具有三个选项:“无延迟”,“显着延迟”和“未知”。我想从此列中删除“未知”选项,然后重新创建我的数据框。

我尝试了这个: toRemove =“未知”

data.frame(lapply(myData,function(communicationDelay)factor(as.character(communicationDelay),levels = levels(communicationDelay)[levels(communicationDelay)!= tomove]))))

在数据查看器中打开数据时,我仍然看到“未知”选项。

我也尝试过:

toRemove =“未知”

mynewData = data.frame(lapply(myData,function(communicationDelay)factor(as.character(communicationDelay),levels = levels(communicationDelay)[levels(communicationDelay)!= toRemove])))

这给了我十列的NA!

抱歉,如果不清楚,这是我的第一个stackoverflow帖子。如果需要,我可以添加更多详细信息!

评论
  • 良德辉
    良德辉 回复

    One option is to use != in subset to filter the rows to remove the value defined in 'toRemove' to be removed from the dataset and as it is a factor, the levels would still be present as unused levels unless we update with droplevels to remove those unused levels

    dat <- droplevels(subset(myData, col1 != toRemove))
    
  • Donne
    Donne 回复

    dplyr解决方案

    library(dplyr)

    数据

    dataframe = tibble(delay = c("No Delay", "Significant Delay", "Unknown"))

    You can remove the unwanted entries by using the filter() function.

    newdataframe = dataframe %>% filter(delay != "Unknown")