[R] How can I access an element of a string?

arun smartpink111 at yahoo.com
Fri Jul 27 08:40:04 CEST 2012



HI,

Try this:
 string1<-"ABCD"
idx<-1:nchar(string1)
 substr(string1,2,idx[2])
[1] "B"
substr(string1,4,idx[4])
[1] "D"
A.K.



----- Original Message -----
From: jpm miao <miaojpm at gmail.com>
To: Daniel Nordlund <djnordlund at frontier.com>; jorgeivanvelez at gmail.com
Cc: r-help <r-help at r-project.org>
Sent: Friday, July 27, 2012 1:57 AM
Subject: [R]  How can I access an element of a string?
fx1<-read.table(text="
    TIME  REER    NTD    JPY      GBP    HKD
1 198001 124.26 36.030 237.96 2.263980 4.8366
2 198002 126.59 36.030 244.05 2.290426 4.8765
3 198003 128.33 36.026 248.62 2.206045 4.9960
4 198004 127.85 36.063 251.67 2.215330 4.9760
5 198005 124.40 36.050 228.35 2.302026 4.8891
6 198006 124.64 36.028 218.05 2.336995 4.9017
7 198007 125.17 36.007 220.95 2.371917 4.9046
8 198008 128.87 35.966 224.45 2.369107 4.9360
",sep="",header=TRUE)
 colnames(fx1)[2]
#[1] "REER"
HI
Dear Daniel and Jorge,

   Thank you very much and it does help.

   If I have a string "ABCD", how can I access the second element of the
string "B"? Thanks,

Miao

2012/7/27 Daniel Nordlund <djnordlund at frontier.com>

> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> > On Behalf Of jpm miao
> > Sent: Thursday, July 26, 2012 9:12 PM
> > To: r-help
> > Subject: [R] How can I access the title of a table read via read.csv?
> >
> > Hi,
> >
> >    I have a table which I can read via read.csv:
> >
> > fx1<-read.csv(file="A_FX_M.csv", header=TRUE)
> >
> >     TIME   REER    NTD    JPY      GBP    HKD
> > 1 198001 124.26 36.030 237.96 2.263980 4.8366
> > 2 198002 126.59 36.030 244.05 2.290426 4.8765
> > 3 198003 128.33 36.026 248.62 2.206045 4.9960
> > 4 198004 127.85 36.063 251.67 2.215330 4.9760
> > 5 198005 124.40 36.050 228.35 2.302026 4.8891
> > 6 198006 124.64 36.028 218.05 2.336995 4.9017
> > 7 198007 125.17 36.007 220.95 2.371917 4.9046
> > 8 198008 128.87 35.966 224.45 2.369107 4.9360
> >
> > .......................
> >
> >
> >   How  can I access the title of the table? For example, I would like
> > to access the character string "REER"; how can I do it?
> >
> >
> >   Thanks,
> >
> >
> > Miao
> >
>
> Look at ?colnames.
>
> colnames(fx1)[2]
>
>
>
> Hope this is helpful,
>
> Dan
>
> Daniel Nordlund
> Bothell, WA USA
>
> ______________________________________________
> 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.
>

    [[alternative HTML version deleted]]

______________________________________________
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.




More information about the R-help mailing list