问题链接看http://www.bio-info-trainee.com/4458.html
我碰到的问题
1.要不要把每个list变成data.frame,我给变成了,我首先想到merge,by ,但是是不对的,merge是两个数据框合并,由于报错一直说"'by'一定得用数字,名字或逻辑值来指定一个或多个列"虽然因为发现字母和rowname是对应的,我又新加id一列为rowname,但是还是报刚才的错。然后我想着我就合并两个data.frame,但是出来的结果是这样的,然后用rbind.fill,也不行。不想放这个自己思考的过程,因为显得"很傻很天真"啊,但没有现在,如何见证后来的成长呢?所以忍痛放在这吧。function(x)
就是软肋啊,看都看不太懂。
e<-merge(a1,a2, by= "id",all = TRUE)
image-20190615185421732
b=rbind.fill(a1,a2,a3,a4,a5)
image-20190615185726894
心情很沮丧,但是我还是要把所有搜索过的网页记录下来,因为虽然没有最后得到结果,但是搜索的过程让我间接学习了其他函数,这次没用上,以后很可能会用的上,一定要把看到眼睛里的东西积累起来,心态要好。
image-20190615191742645 image-20190615191805036 image-20190615191847543 image-20190615191931371 image-20190615192202899 image-20190615192213809 image-20190615192425464 image-20190615192437473 image-20190615192451430 image-20190615192530612 image-20190615193325314 image-20190615193347704 image-20190615193408513 image-20190615193427831 image-20190615194611093 image-20190615194750596image-20190615195000849下面这个示例就可以完美解释上面这张圆饼图了
w1:
NAME SCHOOL CLASS ENGLISH
A S1 10 85
B S2 5 50
A S1 4 90
A S1 11 90
C S1 1 12
w2:
NAME SCHOOL CLASS MATHS ENGLISH
A S3 5 80 88
B S2 5 89 81
C S1 1 55 32
merge(w1, w2, all = T)
NAME SCHOOL CLASS ENGLISH MATHS
1 A S1 4 90 NA
2 A S1 10 85 NA
3 A S1 11 90 NA
4 A S3 5 88 80
5 B S2 5 50 NA
6 B S2 5 81 89
7 C S1 1 12 NA
8 C S1 1 32 55
merge(w1, w2, by = c("NAME", "SCHOOL", "CLASS"), all = T)
NAME SCHOOL CLASS ENGLISH.x MATHS ENGLISH.y
A S1 4 90 NA NA
A S1 10 85 NA NA
A S1 11 90 NA NA
A S3 5 NA 80 88
B S2 5 50 89 81
C S1 1 12 55 32
merge(w1, w2, all = T, incomparables = "A")
Error in merge.data.frame(w1, w2, all = T, incomparables = "A") :
'incomparables' is supported only for merging on a single column
merge(w1, w2, all = T, by = "NAME", incomparables = "A")
NAME SCHOOL.x CLASS.x ENGLISH.x SCHOOL.y CLASS.y MATHS ENGLISH.y
A S1 10 85 <NA> NA NA NA
A S1 4 90 <NA> NA NA NA
A S1 11 90 <NA> NA NA NA
A <NA> NA NA S3 5 80 88
B S2 5 50 S2 5 89 81
C S1 1 12 S1 1 55 32
image-20190615202915877
image-20190615202931925
这个实在看不懂,apply 家族的,收藏http://blog.sina.com.cn/s/blog_6caea8bf0100xkpg.html
image-20190615211051857 image-20190615211128585 image-20190615211152870 image-20190615211838817 image-20190615211906431 image-20190615211923618 image-20190615211950773这个也是很好的,apply家族的,收藏http://blog.fens.me/r-apply/
R数据存储类型,收藏https://zhuanlan.zhihu.com/p/29891963
R语言初学者指南-简单函数https://www.cnblogs.com/Martin-9/p/5387134.html
目前收集了三份答案,学习用
rm(list = ls())
set.seed(0.12345)
n=26
df=data.frame(LETTERS[1:n],rnorm(n),rnorm(n),
rnorm(n),rnorm(n),rnorm(n))
a=lapply(2:ncol(df), function(i){
x=df[,c(1,i)]
x=x[x[,2]>0,]
return(x)
})
#老大
n=sort(unique(unlist(lapply(a, function(x)x[,1]))))
df_new = do.call(cbind,lapply(a, function(x){
b=rep(0,length(n))
b[match(x[,1],n)]=x[,2]
return(b)
}))
rownames(df_new)=n
#小洁(1)
pd=function(x){
p=x[2]
for (i in 1:n){
m[i] <- ifelse(i %in% rownames(p),p[rownames(p)==as.character(i),],0)
}
print(m)
}
mt=sapply(a,pd)
rownames(mt)=LETTERS
colnames(mt)=NULL
mt
#小洁(2)
x=data.frame(LETTERS)
ma <- function(y){
k=merge(x, y, by.x = "LETTERS",by.y = "LETTERS.1.n.",all.x = TRUE)
k[is.na(k)==T] <- 0
print(k[,2])
}
b=sapply(a,ma)
写在最后,虽然做不出来,但是目前的首要任务不是做出来,而是能看懂,就是向前进啦,加油
网友评论