user2585945
user2585945

Reputation: 139

Converting a string to a sequence in R

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

Upvotes: 3

Views: 1167

Answers (2)

user1609452
user1609452

Reputation: 4444

In this case you can coerce your arguments into a form that dget can handle

aTxt <- 'SUB DEP
1   ""
2   "1"
3   "1, 2"
4   "1, 2, 3"
5   "1:3, 5
'
Dependencies <- read.table(text = aTxt, header = TRUE,
                           colClasses = c("numeric", "character"),
                           fill = TRUE)
Dependencies$DEP <- sapply(Dependencies$DEP, function(x) dget(textConnection(paste('c(', 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

Upvotes: 5

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193677

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

Upvotes: 8

Related Questions