Skip to content

How can I access an element of a string?

3 messages · John, arun, Henrik Singmann

#
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>
??? [[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.
#
Dear Miao,

substr() ius waht you want.

substr("ABCD", 2,2)
[1] "B"

Cheers,
Henrik

jpm miao schrieb: