Converting a string to a sequence in R
parsing, r
Solution
Here is a solution using the dreaded `eval(parse(...))` structure:
Dependencies$DEP <- sapply(paste("c(", Dependencies$DEP, ")"),
function(x) eval(parse(text = x)))
Dependencies
# SUB DEP
# 1 1 NULL
# 2 2 1
# 3 3 1, 2
# 4 4 1, 2, 3
# 5 5 1, 2, 3, 5
str(Dependencies)
# 'data.frame': 5 obs. of 2 variables:
# $ SUB: int 1 2 3 4 5
# $ DEP:List of 5
# ..$ c( ) : NULL
# ..$ c( 1 ) : num 1
# ..$ c( 1, 2 ) : num 1 2
# ..$ c( 1, 2, 3 ): num 1 2 3
# ..$ c( 1:3, 5 ) : num 1 2 3 5
Problem
I am reading a table into R of the form ``` SUB DEP 1 "" 2 "1" 3 "1, 2" 4 "1, 2, 3" 5 "1:3, 5" ``` Then I am parsing the DEP variable into a list of numbers as follows: ``` Dependencies <- read.table("dependencies.txt", header = TRUE, colClasses = c("numeric", "character"), fill = TRUE) Dependencies$DEP <- strsplit(Dependencies$DEP, ", ") Dependencies$DEP <- lapply(Dependencies$DEP, as.numeric) ``` This works fine, except for when the file being read in contains a sequence, such as in row 5. `as.numeric("1:3")` returns `NA`, not 1, 2, 3. How should I convert the string `"1:3, 5"` into the numeric vector c(1, 2, 3, 5). I could change how the vector is written in the input files, if that helps. Thanks for the help! Michael