dimanche 12 février 2017

How do I split a string into separate variables by the second colon?

Vote count: 0

I have a string:

 [1] "x: user1 : value1"
 [2] ": user2 : value2"
 [3] ": user2 value3"
 [4] ": user4 : value4"

I need to split user id's by the second colon into a user_id variable, and the text after the second colon into a var1 variable. I also need to add a class variable to each observation, that will be populated with a if there were two colons. If there is no second colon in the string, then I need the class variable to be populated with b.

The dataframe I need at the end looks like this:

user_id    var1       class
  user1    value1       a
  user2    value2       a
  user2    value3       b
  user4    value4       a

Is there a convenient way in R to do this using substr? Thanks!

asked 14 secs ago

Let's block ads! (Why?)



How do I split a string into separate variables by the second colon?

Aucun commentaire:

Enregistrer un commentaire