2
我有兩個data.frames如下:轉換data.frame另一個data.frame
dt2017 = data.frame(id=LETTERS[1:5],year=2017,city1=c(0,1,0,1,0),city2=c(0,0,1,0,0),city3=c(1,0,1,0,1),city4=c(0,0,0,0,1))
dt2017
id year city1 city2 city3 city4
1: A 2017 0 0 1 0
2: B 2017 1 0 0 0
3: C 2017 0 1 1 0
4: D 2017 1 0 0 0
5: E 2017 0 0 1 1
dt2016 = data.frame(id=LETTERS[1:5],year=2016,city1=c(0,0,0,0,1),city2=c(0,0,0,1,0),city3=c(0,0,1,0,1),city4=c(1,1,0,0,1))
dt2016
id year city1 city2 city3 city4|
1: A 2016 0 0 0 1
2: B 2016 0 0 0 1
3: C 2016 0 0 1 0
4: D 2016 0 1 0 0
5: E 2016 1 0 1 1
「1」,在data.frame可以代表這個城市工作。例如,2016年,A,B和E在同一城市工作4。首先,我想以下data.frame:
id 2016 2017 2016+2017
1: A B;E C;E B;C;E
2: B A;E D A;D;E
3: C E A;E A;E
4: D NA B B
5: E A;B;C A;C A;B;C
其次,我希望得到一個data.frame這樣的:
id relation
A B
A C
A E
B A
B D
B E
D B
E A
E B
E C
任何建議,將不勝感激。
[拆分逗號分隔的柱到單獨的行數](的可能的複製https://stackoverflow.com/questions/13773770/split-comma-separated -column-into-separate-rows) – Uwe