Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created April 16, 2012 18:07
Show Gist options
  • Save geofferyzh/2400414 to your computer and use it in GitHub Desktop.
Save geofferyzh/2400414 to your computer and use it in GitHub Desktop.
RinAction - R Data Structure - DataFrame
#################################
# Data Frame #
#################################
# Creating a dataframe
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
patientdata
# Specifying elements of a dataframe
patientdata[1:2]
patientdata[c(2,4)]
patientdata[c("diabetes", "status")]
patientdata$age
# Using factors
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
diabetes <- factor(diabetes)
status <- factor(status, order = TRUE)
patientdata <- data.frame(patientID, age, diabetes, status, row.names=patientID)
str(patientdata)
summary(patientdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment