2016-09-28 101 views
3

我喜欢用条件创建一个带有条件的函数paste0的向量,如果该值大于0,则将该值粘贴到每个rownames中。在R中粘贴条件

ts <- data.frame(t2) 
ts 

1 1 
2 1 
3 0 
4 0 
5 0 
6 2 
footer <- c(paste0("There is ",ts$t2[1]," subject missing in group ",rownames(ts)[1]), 
paste0("There is ",ts$t2[2]," subject missing in group ",rownames(ts)[2]), 
paste0("There are ",ts$t2[6]," subjects missing in group ",rownames(ts)[6])) 
footer 
[1] "There is 1 subject missing in group 1" "There is 1 subject missing in group 2" 
[3] "There are 2 subjects missing in group 6" 

感谢您的支持。

+1

子集,其中的变量不等于零第一行的switch。 – alistaire

+0

@ alistaire,谢谢,现在很容易 – BIN

回答

7

怎么样一个可爱的小sapply与抛出的良好措施

ts <- data.frame(t2 = c(1,1,0,0,0,2)) 

s <- which(rowSums(ts) > 0) 

sapply(s, function(x) paste0("There ", switch(ts[x, ], "1" = "is ", "are "), ts[x,], " missing from group ", x)) 

# [1] "There is 1 missing from group 1" "There is 1 missing from group 2" "There are 2 missing from group 6"