R在数据帧中组合了2列

时间:2013-04-10 14:26:49

标签: r

我是R的新手。我有一个数据框,我从postgres数据库中提取了以下结构

       qc_date qc_camera qc_head qc_sensitivity
269 2012-05-04       RED       1            332
270 2012-05-04       RED       2            333
271 2012-05-10       RED       1            318
272 2012-05-10       RED       2            325
273 2012-05-10      BLUE       1            320
274 2012-05-10      BLUE       2            329
275 2012-05-15       RED       1            330
276 2012-05-15       RED       2            332

如何合并第2列和第3列以生成

    qc_date qc_cam_head qc_sensitivity
269 2012-05-04       RED H1            332
270 2012-05-04       RED H2            333
271 2012-05-10       RED H1            318
272 2012-05-10       RED H2            325
273 2012-05-10      BLUE H1            320
274 2012-05-10      BLUE H2            329
275 2012-05-15       RED H1            330
276 2012-05-15       RED H2            332

我可以使用

自行组合2列

cbind(paste(df_date[,2],df[,3], sep=" H"))

如何在保留其他2列

的同时使用此“绑定功能”
structure(list(qc_date = structure(c(15464, 15464, 15470, 15470, 
15470, 15470, 15475, 15475, 15481, 15481, 15481, 15481, 15490, 
15490, 15490, 15490, 15504, 15504, 15504, 15504, 15512, 15512, 
15512, 15512, 15519, 15519, 15519, 15519, 15525, 15525, 15525, 
15525, 15531, 15531, 15531, 15531, 15538, 15538, 15539, 15539, 
15548, 15548, 15548, 15548, 15554, 15554, 15554, 15554, 15561, 
15561, 15561, 15561, 15561, 15567, 15567, 15567, 15567, 15567, 
15567, 15576, 15576, 15576, 15576, 15580, 15580, 15580, 15580, 
15587, 15587, 15587, 15587, 15595, 15595, 15595, 15595, 15601, 
15601, 15601, 15601, 15601, 15609, 15609, 15609, 15609, 15614, 
15614, 15614, 15614, 15621, 15621, 15621, 15621, 15628, 15628, 
15628, 15628, 15628, 15638, 15638, 15638, 15638, 15645, 15645, 
15645, 15645, 15649, 15649, 15651, 15651, 15656, 15656, 15656, 
15656, 15663, 15663, 15664, 15664, 15671, 15671, 15671, 15671, 
15678, 15678, 15678, 15678, 15684, 15684, 15685, 15685, 15692, 
15692, 15692, 15692, 15692, 15692, 15701, 15701, 15701, 15701, 
15705, 15705, 15713, 15713, 15716, 15716, 15719, 15719, 15719, 
15719, 15728, 15728, 15728, 15728, 15733, 15733, 15733, 15733, 
15741, 15741, 15741, 15741, 15741, 15747, 15747, 15747, 15749, 
15749, 15754, 15754, 15754, 15754, 15762, 15762, 15762, 15762, 
15762, 15768, 15768, 15768, 15768, 15768, 15777, 15777, 15778, 
15778, 15789, 15789, 15790, 15790, 15799, 15799, 15799, 15799, 
15804, 15804, 15804, 15804), class = "Date"), qc_camera = c("RED", 
"RED", "RED", "RED", "BLUE", "BLUE", "RED", "RED", "RED", "RED", 
"BLUE", "BLUE", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", 
"RED", "RED", "RED", "RED", "BLUE", "BLUE", "RED", "RED", "BLUE", 
"BLUE", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", 
"RED", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", "RED", 
"RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", "RED", "RED", 
"BLUE", "RED", "BLUE", "RED", "BLUE", "BLUE", "BLUE", "BLUE", 
"RED", "RED", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", 
"RED", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "BLUE", 
"RED", "RED", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", 
"RED", "RED", "RED", "BLUE", "BLUE", "RED", "RED", "BLUE", "BLUE", 
"BLUE", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", 
"RED", "RED", "RED", "BLUE", "BLUE", "RED", "RED", "BLUE", "BLUE", 
"RED", "RED", "BLUE", "BLUE", "RED", "RED", "BLUE", "BLUE", "BLUE", 
"BLUE", "RED", "RED", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", 
"BLUE", "BLUE", "RED", "RED", "BLUE", "BLUE", "RED", "RED", "RED", 
"RED", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", "RED", 
"RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", "RED", "BLUE", 
"BLUE", "BLUE", "RED", "RED", "RED", "RED", "RED", "BLUE", "BLUE", 
"BLUE", "BLUE", "RED", "RED", "BLUE", "BLUE", "BLUE", "RED", 
"RED", "BLUE", "BLUE", "RED", "RED", "RED", "BLUE", "BLUE", "RED", 
"RED", "RED", "RED", "BLUE", "BLUE", "BLUE", "BLUE", "RED", "RED", 
"RED", "RED", "BLUE", "BLUE"), qc_head = c(1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 
1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 
1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 
2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 
1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 
2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 
2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L
), qc_sensitivity = c(332, 333, 318, 325, 320, 329, 330, 332, 
334, 306, 319, 316, 324, 331, 330, 330, 318, 324, 323, 325, 323, 
327, 322, 330, 328, 323, 332, 334, 330, 327, 330, 333, 326, 330, 
326, 331, 327, 331, 325, 324, 319, 322, 323, 325, 323, 325, 318, 
317, 328, 330, 325, 321, 324, 295, 330, 0, 327, 330, 329, 333, 
333, 331, 326, 332, 330, 330, 331, 336, 331, 329, 330, 327, 320, 
324, 320, 0, 330, 328, 324, 323, 330, 324, 332, 335, 310, 306, 
304, 308, 300, 297, 336, 331, 324, 325, 339, 313, 314, 314, 317, 
341, 320, 320, 320, 323, 327, 345, 346, 325, 327, 332, 331, 316, 
333, 323, 321, 336, 330, 329, 333, 330, 330, 334, 328, 319, 313, 
331, 328, 324, 329, 325, 324, 326, 328, 323, 326, 329, 331, 333, 
330, 308, 307, 307, 300, 334, 328, 325, 334, 327, 324, 318, 320, 
317, 320, 324, 321, 322, 313, 0, 289, 280, 294, 266, 340, 338, 
337, 239, 238, 340, 325, 327, 330, 328, 323, 323, 325, 330, 255, 
250, 254, 260, 261, 323, 305, 330, 325, 312, 310, 296, 301, 317, 
311, 298, 303, 335, 348, 296, 336)), .Names = c("qc_date", "qc_camera", 
"qc_head", "qc_sensitivity"), row.names = 269:465, class = "data.frame")

1 个答案:

答案 0 :(得分:1)

使用transform

DF
##      qc_date qc_camera qc_head qc_sensitivity
## 1 2012-05-04       RED       1            332
## 2 2012-05-04       RED       2            333
## 3 2012-05-10       RED       1            318
## 4 2012-05-10       RED       2            325
## 5 2012-05-10      BLUE       1            320
## 6 2012-05-10      BLUE       2            329
## 7 2012-05-15       RED       1            330
## 8 2012-05-15       RED       2            332

transform(DF, qc_cam_head = paste(qc_camera, qc_head, sep = " H"), qc_camera = NULL, qc_head = NULL)
##      qc_date qc_sensitivity qc_cam_head
## 1 2012-05-04            332      RED H1
## 2 2012-05-04            333      RED H2
## 3 2012-05-10            318      RED H1
## 4 2012-05-10            325      RED H2
## 5 2012-05-10            320     BLUE H1
## 6 2012-05-10            329     BLUE H2
## 7 2012-05-15            330      RED H1
## 8 2012-05-15            332      RED H2