r - 在R中粘贴条件

标签 r paste

我喜欢用函数 paste0 创建一个向量,如果值大于 0,则将值粘贴到每个行名中。

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"

感谢您的提前。

最佳答案

一个漂亮的小 sapply 和一个 switch 怎么样?

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"

关于r - 在R中粘贴条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39738572/

相关文章:

r - paste() 函数和变量的排序级别

windows - 一次从文件中粘贴一行(或一段)

r - mutate_at() : must be a 1d atomic vector or a list

r - data.table中按组划分的分位数

MacOS 上的 R 错误 : vector memory exhausted (limit reached? )

Vim - 用可视模式交换列

excel - 有条件复制粘贴

r - 计算 R 中每年没有 N/A 的观察次数

r - 将字母向量拆分为大小相等的向量

java - 阻止在 EXCEL 中使用 PASTE(Java 和 org.apache.poi)