Dear R users, I have a dataframe with one character field, and I would like to create two new fields (columns) in my dataset, by spliting the existing character field into two using an existing substring. ... something that in SAS I could solve e.g. combining substr(which I am aware exist in R) and "index" for determining the position of the pattern within the string. e.g. if my dataframe is ... A B 1 dgabcrt 2 fgrtabc 3 sabcuuu Then by splitting by substring "abc" I would get ... A B B1 B2 1 dgabcrt dg rt 2 fgrtabc fgrt 3 sabcuuu s uuu Do you know how to do this basic string(dataframe) manipulation in R Saludos, Manuel
> x <- 'dfabcxy' > strsplit(x, 'abc')[[1]] [1] "df" "xy">On 10/28/05, ManuelPerera-Chang@fmc-ag.com <ManuelPerera-Chang@fmc-ag.com> wrote:> > > > > > Dear R users, > > I have a dataframe with one character field, and I would like to create > two > new fields (columns) in my dataset, by spliting the existing character > field into two using an existing substring. > > ... something that in SAS I could solve e.g. combining substr(which I am > aware exist in R) and "index" for determining the position of the pattern > within the string. > e.g. if my dataframe is ... > A B > 1 dgabcrt > 2 fgrtabc > 3 sabcuuu > > Then by splitting by substring "abc" I would get ... > > A B B1 B2 > 1 dgabcrt dg rt > 2 fgrtabc fgrt > 3 sabcuuu s uuu > > Do you know how to do this basic string(dataframe) manipulation in R > > Saludos, > > Manuel > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? [[alternative HTML version deleted]]
see sub() and gsub(), but it won't be straightforward as somethink like "string(dataframe)", ie you'll have to 1st treat the character object with sub() or gsub() and 2d rebuils a dataframe. hope this helps Florence. On 10/28/05, ManuelPerera-Chang@fmc-ag.com <ManuelPerera-Chang@fmc-ag.com> wrote:> > > > > > Dear R users, > > I have a dataframe with one character field, and I would like to create > two > new fields (columns) in my dataset, by spliting the existing character > field into two using an existing substring. > > ... something that in SAS I could solve e.g. combining substr(which I am > aware exist in R) and "index" for determining the position of the pattern > within the string. > e.g. if my dataframe is ... > A B > 1 dgabcrt > 2 fgrtabc > 3 sabcuuu > > Then by splitting by substring "abc" I would get ... > > A B B1 B2 > 1 dgabcrt dg rt > 2 fgrtabc fgrt > 3 sabcuuu s uuu > > Do you know how to do this basic string(dataframe) manipulation in R > > Saludos, > > Manuel > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >[[alternative HTML version deleted]]
Hi Jim,
Thanks for your post, I was aware of strsplit, but really could not find
out how i could use it.
I tried like in your example ...
A<-c(1,2,3)
B<-c("dgabcrt","fgrtabc","sabcuuu")
C<-strsplit(B,"abc")> C
[[1]]
[1] "dg" "rt"
[[2]]
[1] "fgrt"
[[3]]
[1] "s" "uuu"
Which looks promissing, but here C is a list with three elements. But how
to create the two vectors I need from here, that is
("dg","fgrt", "s") and
("rt","","uuu")
(or how to get access to the substrings "rt" or "uuu").
Greetings
Manuel
jim holtman
<jholtman at gmail.c To:
"ManuelPerera-Chang at fmc-ag.com" <ManuelPerera-Chang at
fmc-ag.com>
om> cc: r-help at
stat.math.ethz.ch
Subject: Re: [R] splitting a
character field in R
28.10.2005 16:00
> x <- 'dfabcxy'
> strsplit(x, 'abc')
[[1]]
[1] "df" "xy"
>
On 10/28/05, ManuelPerera-Chang at fmc-ag.com <ManuelPerera-Chang at
fmc-ag.com >
wrote:
Dear R users,
I have a dataframe with one character field, and I would like to
create two
new fields (columns) in my dataset, by spliting the existing
character
field into two using an existing substring.
... something that in SAS I could solve e.g. combining substr(which I
am
aware exist in R) and "index" for determining the position of
the
pattern
within the string.
e.g. if my dataframe is ...
A B
1 dgabcrt
2 fgrtabc
3 sabcuuu
Then by splitting by substring "abc" I would get ...
A B B1 B2
1 dgabcrt dg rt
2 fgrtabc fgrt
3 sabcuuu s uuu
Do you know how to do this basic string(dataframe) manipulation in R
Saludos,
Manuel
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
--
Jim Holtman
Cincinnati, OH
+1 513 247 0281
What the problem you are trying to solve?