使用简单的代码在data.frame中加入两个单独的列

时间:2016-01-12 03:01:41

标签: 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   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

我需要加入end.station.idstart.station.id加入data.frame citibike_station_id


> head(stations)
  id citibike_station_id latitude longitude                         label
1  1                  72 40.76727 -73.99393              W 52 St & 11 Ave
2  2                  79 40.71912 -74.00667      Franklin St & W Broadway
3  3                  82 40.71117 -74.00017        St James Pl & Pearl St
4  4                  83 40.68383 -73.97632 Atlantic Ave & Fort Greene Pl
5  5                 116 40.74178 -74.00150               W 17 St & 8 Ave
6  6                 119 40.69609 -73.97803      Park Ave & St Edwards St

因此,最终结果是一个看起来像这样的数据框,显然没有零。

> 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

1 个答案:

答案 0 :(得分:3)

试试这个:

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")