Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in R Managing Variables. Managing variables in R In this session I will show you how to: Rename, drop and keep variables Create new variables.

Similar presentations


Presentation on theme: "Programming in R Managing Variables. Managing variables in R In this session I will show you how to: Rename, drop and keep variables Create new variables."— Presentation transcript:

1 Programming in R Managing Variables

2 Managing variables in R In this session I will show you how to: Rename, drop and keep variables Create new variables in a R dataset

3 Accessing Variables There are two basic ways to access variables. You can reference the column number or the variable number You can reference the variable name.

4 Accessing variables - indexing Data frames in R are special matrices. Matrices have the concept of rows and columns (rXc). To access a cell –X[r,c] To access a column –X[,c] To access a row –X[r,]

5 Access Variables For example, this will access all rows and column 1: fallsurvey[,1] This will access row 1 and all columns: fallsurvey[1,] This will access just the first obs in the first column: fallsurvey[1,1]

6 Managing Variables - Keep There are a few different ways to specify the variables to keep in the data set We can specify specific columns by the column number We can also keep columns by specifying the name

7 Managing Variables - Keep # Keep based on column number # fallsurvey1 <- fallsurvey[,1:2] head(fallsurvey1) str(fallsurvey1) # Keep based on variable name # fallsurvey2 <- fallsurvey[,c("Sem...Year","Adj.GPA","Section","Gen der..")] head(fallsurvey2)

8 Managing Variables - Drop # Drop based on column number # fallsurvey1 <- fallsurvey[,-2] head(fallsurvey1) fallsurvey2 <- fallsurvey[,names(fallsurvey)[c(-2,- 3)]] head(fallsurvey2)

9 Managing Variables - Drop At this time, we have not shown how to drop variables based on the variable name. There are many different ways to do this. We will show a method that uses matrix processing in the optional session.

10 Managing Variables - Renaming To rename a variable, you need to use the “names” function (we will discuss functions in more detail later). Basically, here you are calling the function, identifying the dataset, then identifying the vector (column) that you are renaming: names(fallsurvey)[names(fallsurvey)=="Sem...Year"] <- "Sem/Year" str(fallsurvey)

11 Managing Variables - Creating new variables Variables can be easily created from other variables available in the data set. Care must be taken to ensure the resulting variable appears in the expected location.

12 Managing Variables - Creating new variables total.drinks <- fallsurvey$Drinks.before.Noon+fallsurvey$Drinks.after.Noon head(total.drinks) ls() fallsurvey$total.drinks <- fallsurvey$Drinks.before.Noon+fallsurvey$Drinks.after.Noon head(fallsurvey$total.drinks) head(fallsurvey) Think about the difference between these two sets of code…


Download ppt "Programming in R Managing Variables. Managing variables in R In this session I will show you how to: Rename, drop and keep variables Create new variables."

Similar presentations


Ads by Google