用特定符号和选择性组合两列

  • 本文关键字:两列 组合 选择性 符号 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")

我想在两列"id"one_answers"row"之间加一个符号。我想把向上放在id为1的两列和id为2的向下之间

我知道是否使用粘贴如下:

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

然而,对于我有的任何id,这都会使上升

任何帮助都将不胜感激

您可以使用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)

最新更新