Created
April 16, 2012 20:54
-
-
Save geofferyzh/2401440 to your computer and use it in GitHub Desktop.
RinAction - R Data Manipulation - Sort and Merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ################################################# | |
| ## SORT & MERGE ## | |
| ################################################# | |
| ################### | |
| #Sorting a dataset | |
| ################### | |
| attach(leadership) | |
| newdata <- leadership[order(age), ] | |
| newdata | |
| detach(leadership) | |
| newdata | |
| attach(leadership) | |
| newdata <- leadership[order(gender, -age), ] | |
| newdata | |
| detach(leadership) | |
| newdata | |
| # Rank | |
| rank(c(1,9,3,3,6,4),ties.method="average") | |
| ########## | |
| # Merging | |
| ########## | |
| # Merging horizontally (adding columns) | |
| total <- merge(dataframeA, dataframeB, by="key") | |
| total <- merge(dataframeA, dataframeB, by=c("key1","key2")) | |
| # Merging without a common key (matrix, dataframe) | |
| total <- cbind(A, B) | |
| # Merging vertically (adding rows) | |
| # two data frames must have the same variables, but not necessarily in the same order | |
| total <- rbind(dataframA, dataframeB) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment