I read many posts on splitting strings in R. However, I am running into an error which I think is due to the way the variables were read into R i.e., space after the date in some cases because the ID is shorter. I am trying to split the character variable “VESSELID” into 2 new variables: “vesselID” and “DATE”. Below is a subset of my dataset.
> dput(df)
structure(list(SETID = c(24153L, 24187L, 24215L, 31990L, 31990L,
31995L, 31995L, 31995L, 31996L, 31996L, 31996L, 31997L, 31997L,
32002L, 32002L, 32002L, 32002L, 32003L, 32003L, 32003L), VESSELID = c("6830 2002/08/13 ",
"6830 2002/08/12 ", "6830 2002/08/15 ", "105372 2002/08/23",
"105372 2002/08/23", "104234 2002/07/20", "104234 2002/07/20",
"104234 2002/07/20", "104234 2002/07/21", "104234 2002/07/21",
"104234 2002/07/21", "104234 2002/07/22", "104234 2002/07/22",
"5744 2002/08/14 ", "5744 2002/08/14 ", "5744 2002/08/14 ",
"5744 2002/08/14 ", "5744 2002/08/13 ", "5744 2002/08/13 ",
"5744 2002/08/13 ")), .Names = c("SETID", "VESSELID"), row.names = c(1L,
2L, 3L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L), class = "data.frame")
I did try the following:
library(reshape2)
test <- data.frame(df, colsplit(df$VESSELID, split= " ",names=c("vesselID","DATE")))
However, I get this error message:
Error in colsplit(log21$VESSELID, split = " ", names = c("vesselID", "DATE")) :
unused argument(s) (split = " ")
The split command doesn’t seem to be able to work properly. I don’t know how to fix my character string.
The argument name is not
split, it ispattern:gives :