Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created April 16, 2012 20:54
Show Gist options
  • Select an option

  • Save geofferyzh/2401440 to your computer and use it in GitHub Desktop.

Select an option

Save geofferyzh/2401440 to your computer and use it in GitHub Desktop.
RinAction - R Data Manipulation - Sort and Merge
#################################################
## 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