-1

I have a character vector

x <- "nkiLVkqspmLVAydnaVNLSCkys"

I want to split it into a vector with 25 elements, so that:

x[1] 
# [1] "n"
x[2] 
# [1] "k"
# and so on ...

The only thing I can think of is to do a regex replace any alpha character with alpha and "," and then split on ",". Is there a simpler way to do this?

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
user2909302
  • 103
  • 8
  • I don't understand what you are suggestion Dason? strsplit on my list would just give me a vector with a single element that has all the letters. That is what I am starting with. I understand that if I modify my original string to include commas I can then split on the commas with strsplit. I was asking if there is another way to do this. – user2909302 Dec 20 '15 at 01:40
  • can you please put me out of my misery and tell me HOW to use strsplit in this case? – user2909302 Dec 20 '15 at 01:44
  • Dason - please see below answer for example on how to be helpful to newbies. – user2909302 Dec 20 '15 at 01:50
  • @user2909302 he helped you. Please dont be rude. You should have asked him directly to spoon feed you. – CuriousBeing Dec 20 '15 at 01:52
  • What @Dason was attempting to do was to help you find `example(strsplit)` (also at the bottom of `?strsplit`). There is an example there of exactly what you are attempting to do. i.e RTFM – Rich Scriven Dec 20 '15 at 02:07
  • I did not mean to be rude but since i mentioned strsplit in my question i dont see how telling me to look at strsplit could be construed as helping me. – user2909302 Dec 20 '15 at 02:17
  • @RichardScriven Not even that necessary. It's spelled out if you read the info for the split parameter. So user2909302 did you actually read the documentation? I mean I reiterated it twice so you know... I would hope you would have got the hint and actually read it. – Dason Dec 20 '15 at 03:02
  • @user2909302 Not being spoon fed a simple answer when the answer is in the documentation is a skill that will help you much more in the long run than getting the immediate answer. I do you a much greater service if I attempt to help you learn how to learn than if I just give you the direct answer. And if you put any effort in you would have got the answer from my hint. I'm not trying to be rude. – Dason Dec 20 '15 at 03:08
  • @Dason, you are correct, it is much better to gain the skill of effectively using the documentation. I was looking for a quick answer and should have specified that. I interpreted your answer as aggressive only because you wrote the same response twice. To a frustrated newbie who has spent hours trying to untangle data frames in R the spoonfed answer was the easiest to process. – user2909302 Dec 20 '15 at 03:44

2 Answers2

1

Try this:

x<-"nkiLVkqspmLVAydnaVNLSCkys"
y<-data.frame(strsplit(x,""))

then do:

y[1,1]

or

y[2,1]

and so on ...

CuriousBeing
  • 1,592
  • 14
  • 34
0

We can also use

library(stringr)
y= data.frame(v1=str_extract_all(x, '.')[[1]])
akrun
  • 874,273
  • 37
  • 540
  • 662