这是我第一次问一个问题,希望我能得到你的帮助! 我需要删除,使用R如何删除r中没有超过3个值的行?
基本上我需要摆脱50S,ABCC8的有只有一个或两个基因值的行,并ACAT1因为这些有< 3.
我期望的输出是
非常感谢你!
这是我第一次问一个问题,希望我能得到你的帮助! 我需要删除,使用R如何删除r中没有超过3个值的行?
基本上我需要摆脱50S,ABCC8的有只有一个或两个基因值的行,并ACAT1因为这些有< 3.
我期望的输出是
非常感谢你!
如果这是在data.frame中,则可以使用dplyr
包进行一些操作。我们可以通过Genes
和count
将数据分组到多少个实例。然后我们只需设置过滤条件即可删除记录。
require(dplyr)
df <- data.frame(
Genes=c('50S' ,'abcb1' ,'abcb1' ,'abcb1' ,'ABCC8' ,'ABL' ,'ABL' ,'ABL' ,'ABL' ,'ACAT1' ,'ACAT1'),
Values=c(-0.627323448, -0.226358414, 0.347305901 ,0.371632631 ,0.099485307 ,0.078512979 ,-0.426643782, -1.060270668, -2.059157991, 0.608899174 ,-0.048795611)
)
#group, filter and join back to get subset the data
df %>% group_by(Genes)
%>% summarize(count=n())
%>% filter(count>=3)
%>% inner_join(df)
%>% select(Genes,Values)
按@拉米亚的评论,可以简化它只是:
df %>% group_by(Genes) %>% filter(n()>=3)
你可以这样简化:'df%>%group_by(基因)%>%filter(n()> = 3)' – Lamia
当我输入我的答案时,我认为“必须有更漂亮的方式这样做......“在这里! –
# generating data
x <- c(NA, NA, NA, NA, 2, 3) # has n < 3!
y <- c(1, 2, 3, 4, 5, 6)
z <- c(1 ,2, 3, NA, 5, 6)
df <- data.frame(x,y,z)
colsToKeep <- c() # making empty vector I will fill with column numbers
for (i in 1:ncol(df)) { # for every column
if (sum(!is.na(df[,i]))>=3) { # if that column has greater than 3 valid values (i.e., ones that are not na...
colsToKeep <- c(colsToKeep, i) # then save that column number into this vector
}
}
df[,colsToKeep] # then use that vector to call the columns you want
注意的是,R对待FALSE为0和TRUE为1,所以这是sum()
功能是如何在这里工作。
另一种可能的解决方案通过使用table
:
gene <- c("A","A","A","B","B","C","C","C","C","D")
value <- c(seq(1,10,1))
df<-data.frame(gene,value)
df
gene value
1 A 1
2 A 2
3 A 3
6 C 6
7 C 7
8 C 8
9 C 9
su<-data.frame(table(df$gene))
df_keep <-df[which(df$gene %in% su[which(su$Freq>2),1]),]
df_keep
gene value
1 A 1
2 A 2
3 A 3
6 C 6
7 C 7
8 C 8
9 C 9
你可以提供一个[MCVE]( https://stackoverflow.com/help/mcve)? – jsb
如何从基因列中的代码中获取基因的数量? – G5W
另外,请不要将您的数据作为图片。我们必须再次输入。相反,使用'dput'来获取可以粘贴到问题中的数据的文本版本。如果你的数据太长,试试像'dput(head(MyData,20))' – G5W