我不确定标题是否足够清楚。我有一个dataframe (如下所示),它包含5列中的值。我想要做的是将这个数据帧“分割”成三个类,其中行可以被分配到“高”、“中”、“低”状态。
我的意思是:
高:至少有3列的值是“高”的。
Medium:取值为"medium“,最少3列
Low:值至少为3列的“Low”(或NA)
我猜它涉及两件事,定义3组的分界值,然后将行划分为高、中和低类别……但那只是个猜测
数据文件为available here
tmp = read.table("tmp2.txt", header=TRUE)
head(tmp)
Geneid Hsap Mmul Mmus Rnor Cfam
1 ENSG00000197711 365823.5 243429.20 44337.267 156874.50 128015.0
2 ENSG00000198712 198613.0 NA 47767.767 200176.50 210559.8
3 ENSG00000198899 189421.5 NA NA 283425.50 367112.8
4 ENSG00000198804 182559.5 NA 87301.900 277861.00 324438.0
5 ENSG00000198840 142424.5 NA 8400.457 45844.80 115027.9
6 ENSG00000171564 119147.9 93564.66 6675.290 45938.85 45140.2任何建议都非常感谢,因为我对如何解决这个问题没有丝毫想法!
谢谢,
这是下面的答案:
我现在已经用一个更真实的文件(更多行)替换了这个文件。
tbl <- read.csv("http://db.tt/L2ehGh8", header=FALSE)
colnames(tbl) <- c("Geneid","Hsap","Mmul","Mmus","Rnor","Cfam")使用cut():我有很多0,并且值是静态拉伸的,所以通过使用log,或者这里的asinh,你可以去掉它。
tbl.data <- apply(asinh(tbl.data),2,
function(x) as.numeric(as.factor(cut(x,4))) )
head(tbl.data)
Hsap Mmul Mmus Rnor Cfam
[1,] 2 2 1 1 2
[2,] 2 2 2 2 2
[3,] 1 1 1 1 1
[4,] 1 1 1 1 1
[5,] 2 3 2 2 3
[6,] 2 2 2 2 2另一种方法是使用分位数,正如我所看到的那样。
quantile(tbl.data[,1],0.25)
quantile(tbl.data[,1],0.5)
quantile(tbl.data[,1],0.75)
tbl.data2 <- apply(tbl.data,2,
function(x) as.numeric(as.factor(cut(x,c(-1,
quantile(x, 0.25)+0.0001,
quantile(x,0.5),
quantile(x,0.75), max(x))))))
head(tbl.data2)
Hsap Mmul Mmus Rnor Cfam
[1,] 3 3 3 2 3
[2,] 2 3 4 3 3
[3,] 2 1 1 1 2
[4,] 1 2 1 1 1
[5,] 4 4 4 4 4
[6,] 3 4 4 3 4发布于 2011-08-08 01:43:30
假设您希望通过不对NA进行计数来处理它们,而不是抛出整行:
tbl <- read.table("http://db.tt/Eb6qM4h",header=TRUE)
tbl.data <- subset(tbl,select=-Geneid)
tbl.data <- apply(tbl.data,2,function(x) as.numeric(as.factor(cut(x,3))) )
countLevels <- function(tbl.data,lvl) {
apply(tbl.data,1,function(x) sum( x[!is.na(x)] == lvl ) )
}
tbl.final <- tbl.new <- subset(tbl,select=Geneid)
for(lvl in seq(3) ) {
tbl.new[,paste('Level',lvl)] <- (countLevels(tbl.data,lvl) > 3) * lvl
}
tbl.final$Levels <- rowSums(subset(tbl.new,select=-Geneid))它按如下方式返回data.frame:
> head(tbl.final,20)
Geneid Levels
1 ENSG00000197711 0
2 ENSG00000198712 0
3 ENSG00000198899 0
4 ENSG00000198804 0
5 ENSG00000198840 0
6 ENSG00000171564 1
7 ENSG00000171557 1
8 ENSG00000198727 1
9 ENSG00000163631 0
10 ENSG00000198888 1
11 ENSG00000198695 1
12 ENSG00000198763 1
13 ENSG00000198786 1
14 ENSG00000158874 0
15 ENSG00000138207 1
16 ENSG00000109072 1
17 ENSG00000130203 3
18 ENSG00000106927 1
19 ENSG00000110169 1
20 ENSG00000104760 1https://stackoverflow.com/questions/6974431
复制相似问题