|
|
|
|
What if you import in a spreadsheet with a space in the column name like below?
ABCDEFGHIJ0123456789 |
id <fctr> | First name <chr> | age <dbl> | grade <fctr> | |
---|---|---|---|---|
60006 | Bob | 45 | NA | |
60007 | Linda | 44 | NA | |
60008 | Louise | 12 | seventh | |
60009 | Tina | 13 | eighth | |
60010 | Gene | 11 | sixth |
How do you refer to the the First name variable/column with $ and not the []?
burgers$`First name`
Let’s create a new data frame burgers2 from burgers because we’re going to change it up.
Run the command on the First name variable/column to strip out the spaces and characters and replace them with periods.
burgers2 <- burgers
colnames(burgers2) <- make.names(colnames(burgers2))
burgers2
Use the special dplyr function to rename the id variable in burgers to employee_id
burgers2 <- rename(burgers, employee_id=id)
burgers2
ABCDEFGHIJ0123456789 |
employee_id <fctr> | name <chr> | age <dbl> | grade <fctr> | |
---|---|---|---|---|
60006 | Bob | 45 | NA | |
60007 | Linda | 44 | NA | |
60008 | Louise | 12 | seventh | |
60009 | Tina | 13 | eighth | |
60010 | Gene | 11 | sixth |
Create a new data frame called kids by filtering out the NAs from grade column in burgers.
kids <- filter(burgers2, !is.na(grade))
kids