使用简单的代码连接data.frame中的两个独立列

使用简单的代码连接data.frame中的两个独立列,r,merge,data.table,R,Merge,Data.table,我需要一个简单的代码来连接来自同一关键数据帧的两个独立列 我的第一个data.frame如下所示: > head(data) bikeid end.station.id start.station.id diff.time stoptime starttime 4 15941 259 315 564 2014-09-02 17:59:30 2014-09-02 18:08:54 8

我需要一个简单的代码来连接来自同一关键数据帧的两个独立列

我的第一个data.frame如下所示:

> head(data) bikeid end.station.id start.station.id diff.time stoptime starttime 4 15941 259 315 564 2014-09-02 17:59:30 2014-09-02 18:08:54 8 15941 229 450 2616 2014-09-09 09:28:39 2014-09-09 10:12:15 9 15941 477 465 3223 2014-09-09 15:59:23 2014-09-09 16:53:06 10 15941 319 147 570 2014-09-09 18:55:44 2014-09-09 19:05:14 14 15941 3002 304 3208 2014-09-12 14:54:10 2014-09-12 15:47:38 19 15941 469 267 2514 2014-09-25 16:13:24 2014-09-25 16:55:18 midtime 4 2014-09-02 18:04:12 8 2014-09-09 09:50:27 9 2014-09-09 16:26:14 10 2014-09-09 19:00:29 14 2014-09-12 15:20:54 19 2014-09-25 16:34:21 所以最终的结果是一个看起来像这样的数据帧,显然没有零

> head(data) bikeid end.station.id start.station.id diff.time stoptime starttime 4 15941 259 315 564 2014-09-02 17:59:30 2014-09-02 18:08:54 8 15941 229 450 2616 2014-09-09 09:28:39 2014-09-09 10:12:15 9 15941 477 465 3223 2014-09-09 15:59:23 2014-09-09 16:53:06 10 15941 319 147 570 2014-09-09 18:55:44 2014-09-09 19:05:14 14 15941 3002 304 3208 2014-09-12 14:54:10 2014-09-12 15:47:38 19 15941 469 267 2514 2014-09-25 16:13:24 2014-09-25 16:55:18 midtime end.station.lat end.station.lon end.station.name start.station.lat 4 2014-09-02 18:04:12 0 0 0 0 8 2014-09-09 09:50:27 0 0 0 0 9 2014-09-09 16:26:14 0 0 0 0 10 2014-09-09 19:00:29 0 0 0 0 14 2014-09-12 15:20:54 0 0 0 0 19 2014-09-25 16:34:21 0 0 0 0 start.station.lon start.station.name 4 0 0 8 0 0 9 0 0 10 0 0 14 0 0 19 0 0 >总目(数据) bikeid end.station.id start.station.id差异时间停止时间starttime 4 15941 259 315 564 2014-09-02 17:59:30 2014-09-02 18:08:54 8 15941 229 450 2616 2014-09-09 09:28:39 2014-09-09 10:12:15 9 15941 477 465 3223 2014-09-09 15:59:23 2014-09-09 16:53:06 10 15941 319 147 570 2014-09-09 18:55:44 2014-09-09 19:05:14 14 15941 3002 304 3208 2014-09-12 14:54:10 2014-09-12 15:47:38 19 15941 469 267 2514 2014-09-25 16:13:24 2014-09-25 16:55:18 midtime end.station.lat end.station.lon end.station.name start.station.lat 4 2014-09-02 18:04:12 0 0 0 0 8 2014-09-09 09:50:27 0 0 0 0 9 2014-09-09 16:26:14 0 0 0 0 10 2014-09-09 19:00:29 0 0 0 0 14 2014-09-12 15:20:54 0 0 0 0 19 2014-09-25 16:34:21 0 0 0 0 start.station.lon start.station.name 4 0 0 8 0 0 9 0 0 10 0 0 14 0 0 19 0 0 试试这个:

data <- data.frame(end.station.id = c(1,2,3), start.station.id = c(3,1,2))
stations <- data.frame(citibike_station_id = c(1,2,3), label = c("One", "Two", "Three"))
data <- merge(data, stations, by.x = "start.station.id", by.y = "citibike_station_id", all.x = TRUE)
data <- merge(data, stations, by.x = "end.station.id",   by.y = "citibike_station_id", all.x = TRUE)
names(data)[3:4] <- c("start", "end")

数据,到目前为止,您尝试了什么?示例不可复制,请阅读如何提出R问题,如何使其可复制?通过添加
dput
?谢谢!我不得不稍微调整一下,但我明白了主要的意思
data <- data.frame(end.station.id = c(1,2,3), start.station.id = c(3,1,2))
stations <- data.frame(citibike_station_id = c(1,2,3), label = c("One", "Two", "Three"))
data <- merge(data, stations, by.x = "start.station.id", by.y = "citibike_station_id", all.x = TRUE)
data <- merge(data, stations, by.x = "end.station.id",   by.y = "citibike_station_id", all.x = TRUE)
names(data)[3:4] <- c("start", "end")