我在R中有两列:
Origin Dest
ALB ATL
ALB LAG
ALB LAX
我需要它们看起来像这样(在一栏中):
Origin-Dest
ALB-ATL
ALB-LAG
ALB-LAX
有没有人知道如何组合两条线而不会太复杂?
这是我到目前为止的代码:
air <- read.table(delta, header=T, sep=",")
aircolSQL <- sqldf("select Origin, Dest, ActualElapsedTime from air")
airsortSQL <- sqldf("select * from aircolSQL order by Origin asc, Dest")
airsortSQL$ActualTimeHours = round((airsortSQL$ActualElapsedTime/60),1)
airsortSQL$ActualElapsedTime <- NULL
谢谢!
答案 0 :(得分:3)
在SQLite中||
用于字符串连接:
library(sqldf)
sqldf("select Origin || '-' || Dest OD from air")
,并提供:
OD
1 ALB-ATL
2 ALB-LAG
3 ALB-LAX
数据我们将其用作air
:
air <- structure(list(Origin = structure(c(1L, 1L, 1L), .Label = "ALB",
class = "factor"), Dest = structure(1:3, .Label = c("ATL", "LAG", "LAX"),
class = "factor")), .Names = c("Origin", "Dest"), class = "data.frame",
row.names = c(NA, -3L))
答案 1 :(得分:2)
如果df
是您的数据框:
df <- data.frame(Origin = rep('ALB', 3), Dest = c('ATL', 'LAG', 'LAX'))
library(tidyr)
unite_(df, '`Origin-Dest`', c('Origin', 'Dest'), sep = "-")
`Origin-Dest`
1 ALB-ATL
2 ALB-LAG
3 ALB-LAX
答案 2 :(得分:1)
我经常使用粘贴:
DataFrame$OriginDest<- paste(DataFrame$Origin,DataFrame$Dest, sep = '-')
我发现这是最不麻烦的方法。