简体   繁体   English

删除少于3个观察值的ar表中的行

[英]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. 但是,似乎以下相同的基本方法对于matrixdata.frametable数据结构同样适用。

#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通常喜欢数据为长格式,通常是通过reshape2melt()命令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.

相关问题 R:删除变量(列)内的观察值(行) - R: Deleting observations (rows) within a variable (column) 删除少于1000个观察值的数据框的列 - Deleting columns of a data frame with less than 1000 observations R:删除某些观察值(行)并从数字中减去变量(列)和 - R: Deleting certain observations (rows) and subtracting a variable (column) from a number and 在 R 中,根据与另一个表的匹配和日期小于另一个表来过滤一个表的行 - In R filter rows of one table based off of matches to another table AND date less than other table 当某些观察值少于 n 行时,使用 dplyr 在数据框中每组采样 n 个随机行 - Sample n random rows per group in a dataframe with dplyr when some observations have less than n rows 如何使训练数据观察类似于我在 R 中的测试数据? 我的观察比预期的要少 - How to make train data observation similar to my test data in R ? My observations are less than it supposed to be 如何在 R 中排除少于 n 个观察值的构面环绕/网格 - How to exclude facet wrap/grid with less than n number of observations in R 在R中保持行小于实际数 - Keeping the rows with less than bigger than a real number in R 删除少于三个唯一观察值的组 - Remove groups with less than three unique observations Spearman等级相关性少于4个观察值? - Spearman rank correlation with less than 4 observations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM