[英]Deleting rows in a r table with less than 3 observations
I'm looking for a way to remove rows in a data frame with less than 3 observations. 我正在寻找一种删除少于3个观察值的数据框中的行的方法。 Let me explain the matter in a better way.
让我以更好的方式解释这件事。 I have a dataframe with 6 indipendent variables and 1 dependent.
我有一个包含6个独立变量和1个从属变量的数据框。 As I'm doing a density plot in
ggplot2
using faceting, variables with less than 3 observations are not plotted (obviously). 当我使用刻面在
ggplot2
进行密度图绘制时,观察值少于3的变量未绘制(很明显)。 I'm looking for a way to delete these rows with less than 3 observations. 我正在寻找一种删除少于3个观察值的行的方法。 this is an example of the data:
这是数据的一个示例:
'data.frame': 432 obs. of 6 variables:
$ ID : Factor w/ 439 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Forno : Factor w/ 8 levels "Micro","Macro",..: 1 1 1 6 6 6 4 4 4 5 ...
$ Varieta: Factor w/ 11 levels "cc","dd",..: 11 11 11 6 6 6 1 1 1 6 ...
$ Impiego: Factor w/ 5 levels "aperto","chiuso",..: 2 2 2 3 3 3 2 2 2 5 ...
$ MediaL : num 60.7 58.9 60.5 55.9 56.1 ...
$ MediaL.sd : num 4.81 4.79 4.84 5.27 5.64 ...
ggplot code: ggplot代码:
ggplot(d1,aes(MediaL))+geom_density(aes(fill=Varieta),colour=NA,alpha=0.5)+
scale_fill_brewer(palette="Set1")+facet_grid(Forno~Impiego)+
theme(axis.text.x=element_text(angle=90,hjust=1))+theme_mio +xlim(45,65)+
stat_bin(geom="text",aes(y=0,label=..count..),size=2,binwidth=2)
I would like to remove all the interactions with less than 3 observations. 我想删除少于3个观察值的所有交互。
Providing the actual output of your sample data would be useful. 提供样本数据的实际输出将很有用。 You can provide this via
dput(yourObject)
instead of the text representation you provided. 您可以通过
dput(yourObject)
而不是您提供的文本表示来提供此功能。 However, it does seem like the same basic approach below works equally well with a matrix
, data.frame
, and table
data structure. 但是,似乎以下相同的基本方法对于
matrix
, data.frame
和table
数据结构同样适用。
#Matrix
x <- matrix(c(5,4,4,3,1,5,1,8,2), ncol = 3, byrow = TRUE)
x[x < 3] <- NA
#----
[,1] [,2] [,3]
[1,] 5 4 4
[2,] 3 NA 5
[3,] NA 8 NA
#data.frame
xd <- as.data.frame(matrix(c(5,4,4,3,1,5,1,8,2), ncol = 3, byrow = TRUE))
xd[xd < 3] <- NA
#----
V1 V2 V3
1 5 4 4
2 3 NA 5
3 NA 8 NA
#Table. Simulate some data first
set.seed(1)
samp <- data.frame(x1 = sample(c("acqua", "fango", "neve"), 20, TRUE),
x2 = sample(c("pippo", "pluto", "paperino"), 20, TRUE))
x2 <-table(samp)
x2[x2 < 3] <- NA
#----
x2
x1 paperino pippo pluto
acqua 3
fango 3
neve 3 3
ggplot generally likes data to be in long format, most often achieved via the melt()
command in reshape2
. ggplot通常喜欢数据为长格式,通常是通过
reshape2
的melt()
命令reshape2
。 If you provide your plotting code, that may illustrate a better way to remove the data you don't want to plot. 如果提供绘图代码,则可以说明一种删除不需要绘图数据的更好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.