English 中文(简体)
无法过滤数据框架?
原标题:Unable to filter a data frame?
  • 时间:2011-10-01 22:25:28
  •  标签:
  • r
  • ggplot2

我正在利用这样的一些东西来过滤我的数据框架:

d1 = data.frame(data[data$ColA == "ColACat1" & data$ColB == "ColBCat2", ])

当I印刷d1时,按预期使用。 然而,当我打1美元时,它仍然从原始数据框架中印刷所有材料。

> print(d1)
ColA     ColB
-----------------
ColACat1 ColBCat2
ColACat1 ColBCat2

> print(d1$ColA)
Levels: ColACat1 ColACat2

也许,如果我通过<条码>d1到批号时,就会看我的图表,不使用过滤器。 是否有任何途径,我可以过滤数据框架并获得,只有。 与过滤器相匹配的记录? 我想d1,不了解/data>。

最佳回答

As you allude to, the default behavior in R is to treat character columns in data frames as a special data type, called a factor. This is a feature, not a bug, but like any useful feature if you re not expecting it and don t know how to properly use it, it can be quite confusing.

<代码>要素系指在统计中经常出现的分类(而不是数字或数量)变量。

您所用的分级业务实际上通常都有效。 也就是说,它们将回复到你数据框架的正确分类。 然而,该变量的<代码>级属性保持不变,其原有水平仍然all<>。

This means that any method written in R that is designed to take advantage of factors will treat that column as a categorical variable with a bunch of levels, many of which just aren t present. In statistics, one often wants to track the presence of missing levels of categorical variables.

我实际上也宁愿与<代码>stringsAsctors = FALSE合作,但许多人因能够减少编码可携带性而fr。 (TRUE 是缺省,因此,与他人分享你的代码可能会有风险,除非你在每一封面上打上电话options。)

一种可能更方便的解决办法,特别是在数据框架方面,是将<条码>下 次<>>>>>> 和<条码>>级功能结合起来:

subsetDrop <- function(...){
    droplevels(subset(...))
}

并且利用这一职能,以可保证的方式提取任何未使用的数据基。

问题回答

这是一种痛苦! 如果你不行使这一权利,就坐下来。 在我的发言稿一开始采用这一办法就解决了这一问题:

options(stringsAsFactors = FALSE)

就像它打算的行为一样,但不幸的是,我出于某种其他目的将这一特征变成了,它开始给我的所有其他文字造成麻烦。





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...