将两列与特定符号和选择性结合起来

时间:2015-03-29 23:47:46

标签: r

我的数据框看起来像下面

df<- structure(list(id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2), avg = c(0.0380166666666667, 0.0427066666666667, 
0.0441, 0.04644, 0.04798, 0.0537566666666667, 0.05833, 0.0628966666666667, 
0.07327, 0.08572, -0.05641, -0.0439433333333333, -0.04056, -0.03524, 
-0.0339933333333333, -0.0333633333333333, -0.0308766666666667, 
-0.0295033333333333, -0.0294633333333333, -0.02782), row = c("210511_s_at", 
"213352_at", "204468_s_at", "37022_at", "217428_s_at", "204619_s_at", 
"216442_x_at", "210517_s_at", "210495_x_at", "211597_s_at", "205891_at", 
"209138_x_at", "217378_x_at", "211798_x_at", "215946_x_at", "216576_x_at", 
"209569_x_at", "214440_at", "221671_x_at", "216557_x_at"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("amiprilose", "tiabendazole", "chlorhexidine", 
"sulconazole", "melatonin", "monorden", "etynodiol", "tracazolate", 
"cefmetazole", "dantrolene", "xylazine", "Prestwick.972", "chenodeoxycholic.acid", 
"Prestwick.674", "betahistine", "asiaticoside", "ioversol", "trimethadione", 
"alprenolol", "oxprenolol", "flavoxate", "pyrimethamine", "pimozide", 
"hecogenin", "sulfamerazine", "sitosterol", "coralyne", "ursodeoxycholic.acid", 
"cefapirin", "levobunolol"), class = "factor"), value = c(0.0248, 
-0.0807, 0.0429, 0.0497, -0.0822, 0.0076, 0.1137, -0.12, -0.0806, 
0.002, -0.1821, 0.2279, -0.1897, -0.1, -0.1769, 0.0062, -0.1226, 
0.0808, 0.0083, -0.0055)), .Names = c("id", "avg", "row", "variable", 
"value"), row.names = c(NA, 20L), class = "data.frame")

我想在两列之间添加一个符号&#34; id&#34; &#34; row&#34; 。我想将 up 放在ID为1的两列和 down 之间,其中id为2

我知道如果使用粘贴如下:

df$label <- paste(df$id, "_up_", df$row, sep = "")

但是,对于我拥有的任何ID

,这都是 up

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

你可以使用ifelse

   df$label<-NA
   df$label <- ifelse(df$id==1,paste(df$id, "_up_", df$row, sep = ""),df$label)
   df$label <- ifelse(df$id==2,paste(df$id, "_down_", df$row, sep = ""),df$label)