I'm new to R, and I'm playing with strsplit
within a data frame. My data frame uses the following:
Student <- c("John Davis","Angela Williams","Bullwinkle Moose","David Jones",
"Janice Markhammer","Cheryl Cushing","Reuven Ytzrhak","Greg Knox","Joel England",
"Mary Rayburn")
Math <- c(502,600,412,358,495,512,410,625,573,522)
Science <- c(95,99,80,82,75,85,80,95,89,86)
English <- c(25,22,18,15,20,28,15,30,27,18)
student.exam.data <- data.frame(Student,Math,Science,English)
I then attempt to use the following to split the Student
using the following:
student.exam.data$Student <- strsplit(student.exam.data$Student, " ", fixed = TRUE)
which produces the following error:
Error in strsplit(student.exam.data$Student, " ", fixed = TRUE) :
non-character argument
The only way I've found to split my Student
column is to first substitute the space with a period, using student.exam.data <- sub("\\s", ".", student.exam.data$Student)
followed by student.exam.data$Student <- strsplit(student.exam.data$Student, ".", fixed = TRUE)
Why does this work this way, and how can I use strsplit
on whitespace?