2

I am trying to capture the first string from a list using an explicit loop, as shown in the following code:

for (loop.temp in (1:nrow(temp)))
    {temp[loop.temp,"drug_name_mod"] <- unlist(strsplit(temp[loop.temp,"drug_name"]," "))[1]
     print(paste(loop.temp,nrow(temp),sep="/"))
     flush.console()
    }

But I think it is not very efficient, anyway of improving it? Thanks!

lokheart
  • 23,743
  • 39
  • 98
  • 169
  • 1
    Check http://stackoverflow.com/questions/3003527/how-do-i-specify-a-dynamic-position-for-the-start-of-substring. Standard solutions are `lapply` over `strsplit`, `gsub` or hadley's stringr package. – Marek Dec 29 '10 at 06:50
  • 1
    It's quite similar to your earlier question http://stackoverflow.com/questions/3605527/how-to-pick-up-a-set-of-numbers-from-the-end-of-lines-with-irregular-length-in-r – Marek Dec 29 '10 at 06:57

1 Answers1

3

First strsplit the strings, this gives you a list of string vectors, then lapply across that to get only the first element, and unlist that:

temp$drug_name_mod <- unlist(lapply(strsplit(temp$drug_name, " "), function(x) x[1]))

sapply makes it slightly simpler:

temp$drug_name_mod <- sapply(strsplit(temp$drug_name, " "), function(x) x[1])

And you can use "[" directly with 1 as its argument rather than an anonymous function:

temp$drug_name_mod <- sapply(strsplit(temp$drug_name, " "), "[", 1)
mdsumner
  • 29,099
  • 6
  • 83
  • 91