I am wondering if there is any function in R that is similar to the "scan" function in SAS. I have a data.frame which has two columns as the following: one two 1 2 3 4 5 6 I used the "paste" function to create the third column: three <- paste(one,'-',two,sep="") so the data.frame is like this now: one two three 1 2 1-2 3 4 3-4 5 6 5-6 My question is, is there any function in R that can do the opposite thing to what "paste" does? suppose I only have the third column "three" now, and I want to get the first and second columns. If in SAS, I can do the following: one = scan(three,1,'-') two = scan(three,-1,'-') How can I do this in R? thank you, karena -- View this message in context: http://r.789695.n4.nabble.com/any-function-in-R-similar-to-the-scan-function-in-SAS-tp2131264p2131264.html Sent from the R help mailing list archive at Nabble.com.
Erik Iverson
2010-May-05 23:57 UTC
[R] any function in R similar to the "scan" function in SAS?
karena wrote:> I am wondering if there is any function in R that is similar to the "scan" > function in SAS. > I have a data.frame which has two columns as the following: > one two > 1 2 > 3 4 > 5 6 > > I used the "paste" function to create the third column: three <- > paste(one,'-',two,sep="") > so the data.frame is like this now: > one two three > 1 2 1-2 > 3 4 3-4 > 5 6 5-6 > > My question is, is there any function in R that can do the opposite thing to > what "paste" does? > suppose I only have the third column "three" now, and I want to get the > first and second columns. If in SAS, I can do the following: > one = scan(three,1,'-') > two = scan(three,-1,'-') > > How can I do this in R??strsplit
Phil Spector
2010-May-06 00:09 UTC
[R] any function in R similar to the "scan" function in SAS?
Karena -
You can use strsplit combined with sapply to do what
you want:
> three = c('1-2','3-4','5-6')
> ss = strsplit(three,'-')
> one = sapply(ss,function(x)x[1])
> two = sapply(ss,function(x)x[2])
> one
[1] "1" "3" "5"> two
[1] "2" "4" "6"
You might want to use as.numeric to convert them back
to numbers, since SAS doesn't care about that particular
detail.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Wed, 5 May 2010, karena wrote:
>
> I am wondering if there is any function in R that is similar to the
"scan"
> function in SAS.
> I have a data.frame which has two columns as the following:
> one two
> 1 2
> 3 4
> 5 6
>
> I used the "paste" function to create the third column: three
<-
> paste(one,'-',two,sep="")
> so the data.frame is like this now:
> one two three
> 1 2 1-2
> 3 4 3-4
> 5 6 5-6
>
> My question is, is there any function in R that can do the opposite thing
to
> what "paste" does?
> suppose I only have the third column "three" now, and I want to
get the
> first and second columns. If in SAS, I can do the following:
> one = scan(three,1,'-')
> two = scan(three,-1,'-')
>
> How can I do this in R?
>
> thank you,
>
> karena
> --
> View this message in context:
http://r.789695.n4.nabble.com/any-function-in-R-similar-to-the-scan-function-in-SAS-tp2131264p2131264.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
>
thank you guys very much! -- View this message in context: http://r.789695.n4.nabble.com/any-function-in-R-similar-to-the-scan-function-in-SAS-tp2131264p2131930.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2010-May-06 01:17 UTC
[R] any function in R similar to the "scan" function in SAS?
You can read from a character vector as if it were a file like this:> x <- c("1-2", "3-4", "5-6") > read.table(textConnection(x), sep = "-", col.names = c("one", "two"))one two 1 1 2 2 3 4 3 5 6 On Wed, May 5, 2010 at 12:45 PM, karena <dr.jzhou at gmail.com> wrote:> > I am wondering if there is any function in R that is similar to the "scan" > function in SAS. > I have a data.frame which has two columns as the following: > one ? two > 1 ? ? ?2 > 3 ? ? ?4 > 5 ? ? ?6 > > I used the "paste" function to create the third column: three <- > paste(one,'-',two,sep="") > so the data.frame is like this now: > one ? two ? three > 1 ? ? ?2 ? ? ? 1-2 > 3 ? ? ?4 ? ? ? 3-4 > 5 ? ? ?6 ? ? ? 5-6 > > My question is, is there any function in R that can do the opposite thing to > what "paste" does? > suppose I only have the third column "three" now, and I want to get the > first and second columns. If in SAS, I can do the following: > one = scan(three,1,'-') > two = scan(three,-1,'-') > > How can I do this in R? > > thank you, > > karena > -- > View this message in context: http://r.789695.n4.nabble.com/any-function-in-R-similar-to-the-scan-function-in-SAS-tp2131264p2131264.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >