我在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
谢谢!
如果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
在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))
我经常使用粘贴:
DataFrame$OriginDest<- paste(DataFrame$Origin,DataFrame$Dest, sep = '-')
我发现这是最不麻烦的方法。