⇤ ← Revision 1 vom 2015-03-15 11:04:41
Größe: 4284
Kommentar:
|
← Revision 2 vom 2015-03-15 11:06:03 ⇥
Größe: 4283
Kommentar:
|
Gelöschter Text ist auf diese Art markiert. | Hinzugefügter Text ist auf diese Art markiert. |
Zeile 52: | Zeile 52: |
== inner join == | === inner join === |
Zeile 60: | Zeile 60: |
== left outer join == | === left outer join === |
Zeile 69: | Zeile 69: |
== right outer join == | === right outer join === |
Zeile 79: | Zeile 79: |
== full outer join == | === full outer join === |
Zeile 90: | Zeile 90: |
== merge() == | |
Zeile 94: | Zeile 94: |
== merge() Exercise == | === merge() Exercise === |
Zeile 97: | Zeile 97: |
== merge() Solution == | === merge() Solution === |
Combining Data Frames
Inhaltsverzeichnis
rbind()
- rbind() can be used to combine two dataframes (or matrices) in the sense of adding rows, the column names and types must be the same for the two objects
cbind()
- cbind() can be used to combine two dataframes (or matrices) in the sense of adding columns, the number of rows must be the same for the two objects
- it is not recommended to use cbind() to combining data frames
merge()
- merge() is the command of choice for merging or joining data frames
- it is the equivalent of join in sql
- there are four cases
- inner join
- left outer join
- right outer join
- full outer join
inner join
- inner join means: keep only the cases present in both of the data frames
left outer join
- left outer join means: keep all cases of the left data frame no matter if they are present in the right data frame (all.x=T)
right outer join
- right outer join means: keep all cases of the right data frame no matter if they are present in the left data frame (all.y=T)
full outer join
- full outer join means: keep all cases of both data frames (all=T)
- if not stated otherwise R uses the intersect of the names of both data frames, in our case only \textit{id}
- you can specify these columns directly by \texttt{by=c("colname1","colname2")} if the columns are named identical or
- using\\ \texttt{by.x=c("colname1.x","colname2.x"),
merge() Exercise
- now read in the file personendaten.txt using the appropriate command
- join the demographics with our pre1 data frame (even though it does not make sense now)
merge() Solution
1 > persdat <- read.table("../session1/session1data/personendaten.txt",
2 + sep="\t",
3 + header=T)
4 > pre1 <- merge(persdat,pre1,all.y = T)
5 > head(pre1)
6 Subject Sex Age_PRETEST Trial Event.Type Code Time TTime Uncertainty
7 1 PRE001 f 3.11 7 Response 2 178963 10009 1
8 2 PRE001 f 3.11 12 Response 1 238680 8342 1
9 3 PRE001 f 3.11 17 Response 2 297789 8066 1
10 4 PRE001 f 3.11 22 Response 1 351321 10811 1
11 5 PRE001 f 3.11 27 Response 2 403607 713 1
12 6 PRE001 f 3.11 32 Response 1 467793 23709 1
13 Duration Uncertainty.1 ReqTime ReqDur Stim.Type Pair.Index Type Event.Code
14 1 10197 2 0 next incorrect 7 Picture RO09.jpg
15 2 8398 2 0 next incorrect 12 Picture RO20.jpg
16 3 8198 2 0 next hit 17 Picture RS28.jpg
17 4 10997 2 0 next hit 22 Picture AT26.jpg
18 5 800 2 0 next hit 27 Picture RS23.jpg
19 6 23794 2 0 next hit 32 Picture OF04.jpg