R总FUN =头 [英] R Aggregate FUN=head
问题描述
我想通过两列(顺序和程序)聚合一个表(选项卡),以获取采样大小的第一行(FUN = head)。
I would like to aggregate a table (tab) by two columns (sequence and program) to get the top row of samplesize (FUN=head).
sq <- c(1,1,1,1,1,1)
prog<- c('A','A','B','B','C','C')
ss <- c(47,47,28,28,47,47)
tab<- data.frame(sq,prog,ss)
总体给了我一个奇怪的结果
Aggregate is giving me an odd result in that if the sample size is the same for a DIFFERENT combination of sequence and program- it omits it.
agg <- aggregate(cbind(sq,prog) ~ ss, data = tab, FUN=head,1,na.rm=TRUE)
我很困惑为什么发生这种情况,以及为什么当它是文本(A,B,C)时它将程序更改为数字序列。
I'm confused why this is occurring and why it is changing the program to a numerical sequence when it is text (A,B,C).
推荐答案
这是因为默认情况下, data.frame
会从字符列。您需要:
It's because by default, data.frame
creates a factor from character columns. You need:
tab <- data.frame(sq, prog, ss, stringsAsFactors = FALSE)
编辑:我个人认为 dplyr
软件包非常直观。为了您的结果,我将使用:
I personally find the dplyr
package very intuitive. For your result, I'd use:
library(dplyr)
tab %>%
group_by(sq, prog) %>%
filter(row_number() == 1)
这篇关于R总FUN =头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!