I have two data frames from a study, datlighton and datlightoff, They both have roughly the same data since what separates the observations is that off takes place before midnight and on takes place after. I need to combine them into a single data frame called datlight but I'm not sure how to do it. I've tried using cbind and merge but I'm new to R and I don't quite understand how to make it do exactly what I want. When I try merge(datlighton,datlightoff)
it gives me a data frame with all the column names but none of the rows. This is datlighton and datlightoff, I converted it to an html since I don't know to upload them as dataframes from R. Basically I just need to put all the rows from one frame into the other and have them match up with the appropriate column name.
Asked
Active
Viewed 1,155 times
-2

Britta
- 1
- 2
-
3Links to files on your desktop are not going to work. Please create some example data using `dput(head(datlighton))` and the equivalent for the other data. – thelatemail Apr 10 '16 at 22:57
-
1those links are to the files on your computer. can you paste in the results of `dput(head(datlighton))` and `dput(head(datlightoff))` instead? – jaimedash Apr 10 '16 at 22:57
1 Answers
0
a=data.frame(a=4:6,b=2:4,d=7:9)
f=data.frame(a=4:7,b=2:5,c=9:6,d=0:3,e=6:9)
Result = merge(a,f,by="row.names",all=TRUE)
a
a b d
1 4 2 7
2 5 3 8
3 6 4 9
f
a b c d e
1 4 2 9 0 6
2 5 3 8 1 7
3 6 4 7 2 8
4 7 5 6 3 9
Result
Row.names a.x b.x d.x a.y b.y c d.y e
1 1 4 2 7 4 2 9 0 6
2 2 5 3 8 5 3 8 1 7
3 3 6 4 9 6 4 7 2 8
4 4 NA NA NA 7 5 6 3 9
Result[is.na(Result)] <- 0
Result
Row.names a.x b.x d.x a.y b.y c d.y e
1 1 4 2 7 4 2 9 0 6
2 2 5 3 8 5 3 8 1 7
3 3 6 4 9 6 4 7 2 8
4 4 0 0 0 7 5 6 3 9

Sowmya S. Manian
- 3,723
- 3
- 18
- 30