On Jan 22, 2012, at 2:31 PM, Sam Steingold wrote:
* Bert Gunter <[email protected]> [2012-01-20 11:06:31 -0800]:
On Fri, Jan 20, 2012 at 10:52 AM, Sam Steingold <[email protected]> wrote:
then I need to split the two strings by 6/8 characters -- how?
This makes no sense to me. strsplit takes care of this.
I want to convert
c("abcd","de","fghijk")
[1] "abcd" "de" "fghijk"
to
[1] "ab" "cd" "de" "fg" "hi" "jk"
i.e., split strings into substrings of a given length (2 in the above
example, 9 in my real problem).
> unlist( strsplit( gsub("(..)", "\\1,", c("abcd","de","fghijk")),
"," ) )
[1] "ab" "cd" "de" "fg" "hi" "jk"
Change .. to .{9} for you problem.
actually, better yet, from
data.frame(id=1:3,data=c("abcd","de","fghijk"))
id data
1 1 abcd
2 2 de
3 3 fghijk
rep(1:3, lapply( strsplit( gsub("(..)", "\\1,",
c("abcd","de","fghijk")), "," ) , length)
+ )
[1] 1 1 2 3 3 3
data.frame(id = rep(1:3, lapply( strsplit( gsub("(..)", "\\1,",
c("abcd","de","fghijk")), "," ) , length) ),
data= unlist( strsplit( gsub("(..)", "\\1,",
c("abcd","de","fghijk")), "," ) )
)
id data
1 1 ab
2 1 cd
3 2 de
4 3 fg
5 3 hi
6 3 jk
to
id data
1 1 ab
2 1 cd
3 2 de
4 3 fg
5 3 hi
6 3 jk
--
David Winsemius, MD
West Hartford, CT
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.