0

I'm trying to split a column in a dataset based on a " " rather than a "," or some other delimiter. Is this possible?

so for example I have:

VVV <- strsplit(df$col, " ")

however I get the error: Error in strsplit(df$col, " ") : non-character argument I'd like to separate based on the space right before the new set of characters begin. It doesn't help that a few rows have 2 spaces.

aaaa bbbb

aaa bbbb

caaa hhh

What should I do instead?

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 2
    You have a `factor` i'm guessing - try `strsplit(as.character(df$col), " ")` – thelatemail Jan 04 '17 at 23:28
  • And if you set `split = " +"` in your `strsplit` it will split on *one or more* spaces. – Gregor Thomas Jan 04 '17 at 23:30
  • or `strsplit(as.character(df$col), split="\\s+")` if your spaces happen to be something other than a simple space character (e.g. - tabs etc) – thelatemail Jan 04 '17 at 23:32
  • @thelatemail the latest line made a new item under the "Values" header in the environment tab however it didn't split the tab in the dataframe itself. Was hoping to also be able to rename the new columns created by the split as well. If it isn't too much to ask. – StargazingFish Jan 04 '17 at 23:41
  • @StargazingFish - you will have to assign the result back to your data.frame - potentially in parts. You may also want to look at the `separate()` function in the `dplyr` package. – thelatemail Jan 04 '17 at 23:48
  • And you can rename columns created by the split just like you would rename any other columns. I second the `separate` recommendation - it's very friendly - but you'll find it in the `tidyr` package not in `dplyr`. – Gregor Thomas Jan 05 '17 at 00:05
  • How do I reassign the result back? And I've tried the separate that doesn't do it for me. And would like to focus on this method. – StargazingFish Jan 05 '17 at 00:12
  • @StargazingFish - try this answer - http://stackoverflow.com/a/4350905/496803 – thelatemail Jan 05 '17 at 00:19
  • right well i have no idea how to relate that to what I was doing. – StargazingFish Jan 05 '17 at 00:32
  • Use str_split from package "stringr". The function has option of Simplify = T which will give you output as a matrix which can easily rename. – wololo Jan 05 '17 at 06:47

0 Answers0