我有一个大数据框,我想根据条件将字符附加到行名。我有以下示例:
trees <- data.frame(char = c('flower', 'cone', 'flower', 'cone'), number = c(3, 3, 5, 6))
rownames(trees) <- c('birch', 'pine', 'maple', 'redwood')
这就是我想要的,松树和红木旁边的“c”:
char number
birch flower 3
pine c cone 3
maple flower 5
redwood c cone 6
我知道我可以使用粘贴来附加字符:
# this gives the output I am looking for
paste(rownames(trees[trees$char == 'cone',]), 'c')
[1] "pine c" "redwood c"
但是,当我尝试以下代码行时,更改不会出现在我的数据框中:
rownames(trees[trees$char == 'cone',]) <- paste(rownames(trees[trees$char == 'cone',]), 'c')