在R中对数据帧进行子集设置

在R中对数据帧进行子集设置,r,dataframe,subset,R,Dataframe,Subset,我在R中设置数据帧子集时遇到问题。 数据帧如下所示 carat cut color clarity depth table price x y z 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E V

我在R中设置数据帧子集时遇到问题。 数据帧如下所示

  carat       cut color clarity depth table price    x    y    z
1  0.23     Ideal     E     SI2  61.5    55   326 3.95 3.98 2.43
2  0.21   Premium     E     SI1  59.8    61   326 3.89 3.84 2.31
3  0.23      Good     E     VS1  56.9    65   327 4.05 4.07 2.31
4  0.29   Premium     I     VS2  62.4    58   334 4.20 4.23 2.63
5  0.31      Good     J     SI2  63.3    58   335 4.34 4.35 2.75
6  0.24 Very Good     J    VVS2  62.8    57   336 3.94 3.96 2.48
subset(diamonds, color = D)
我用来将其子集的代码如下所示

  carat       cut color clarity depth table price    x    y    z
1  0.23     Ideal     E     SI2  61.5    55   326 3.95 3.98 2.43
2  0.21   Premium     E     SI1  59.8    61   326 3.89 3.84 2.31
3  0.23      Good     E     VS1  56.9    65   327 4.05 4.07 2.31
4  0.29   Premium     I     VS2  62.4    58   334 4.20 4.23 2.63
5  0.31      Good     J     SI2  63.3    58   335 4.34 4.35 2.75
6  0.24 Very Good     J    VVS2  62.8    57   336 3.94 3.96 2.48
subset(diamonds, color = D)
无论出于何种原因,未正确过滤子集数据帧。当我在后记中看到它时,它包含的颜色不仅仅是字母D。有人能向我解释为什么会发生这种情况吗?还有我如何修复它

请记住,我对R很陌生

subset(diamonds, color == 'D')

这是你的子集,在文档中有很好的介绍

对于可读性,Sidhha的选项是好的,但一些人认为,包括在
子集的帮助文件中的警告,最好使用标准的子集函数,如
[
。Hadley Wickham对此进行了讨论

因此,使用Heroka在评论中写道的标准子集函数
[
,有时更可取:

diamonds[diamonds$color == "D", ]
subset(diamonds,color==“D”)
diamonds[diamonds$color==“D”,]
。签出
?“[.data.frame”