I have two columns in R:
Origin Dest
ALB ATL
ALB LAG
ALB LAX
I need them to look like this (in one column):
Origin-Dest
ALB-ATL
ALB-LAG
ALB-LAX
Does anyone know how to combine two lines without getting too complicated?
This is the code I have so far:
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
Thanks!
In SQLite ||
is used for string concatenation:
library(sqldf)
sqldf("select Origin || '-' || Dest OD from air")
giving:
OD
1 ALB-ATL
2 ALB-LAG
3 ALB-LAX
data We used this as 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))
If df
is your data frame:
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
I often use paste:
DataFrame$OriginDest<- paste(DataFrame$Origin,DataFrame$Dest, sep = '-')
I find it the least hassle approach.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.