R字符子集数据框

时间:2019-05-14 15:18:50

标签: r dataframe subset

我正在尝试使用csv文件从数据帧创建一个子集。要应用的过滤器是一个字符。这是编写的代码:

project_subset = subset (x = fed_stimulus, subset = 'Project Status' == "Completed 50% or more", select = 'Project Name')

该代码不会返回任何错误,但不会创建一个子集。请帮助

2 个答案:

答案 0 :(得分:0)

之所以没有创建子集,是因为该行

'Project Status' == "Completed 50% or more"

您只是比较两个不相等的字符串。这将始终为FALSE,并且子集会寻找TRUE个要过滤的案例。

您需要做的是取消列名的引用,或者将其作为数据引用传递。

#unquoted variable name
project_subset = subset (x = fed_stimulus, subset = Project Status == "Completed 50% or more", select = 'Project Name')

# quoted variable name but used as a column reference from your original data
project_subset = subset (x = fed_stimulus, subset = fed_stimulus[ ,"Project Status"] == "Completed 50% or more", select = 'Project Name')

答案 1 :(得分:0)

当列名带有空格时,必须将其放在反引号`中:

project_subset = subset (x = fed_stimulus, subset = `Project Status` == "Completed 50% or more", select = 'Project Name')

但是,如documentation所述,subset()旨在作为一种便于交互使用的便利功能,因此,如果您打算在脚本中使用此功能,则最好像这样使用[这个:

project_subset = fed_stimulus[fed_stimulus$`Project Status` == "Completed 50% or more", "Project Name"]