Is there a way to convince R to create a character vector without using the
quotes?
This works
ex1 <- c("first","second")
but when I try this it doesn't
ext <- as.character(c(first,second))
it complains. I have many variables to put into character vectors so dispensing
with the quotes would be useful.
Thanks
Jim
==============================Dr. Jim Maas
University of East Anglia
[[alternative HTML version deleted]]
On 03/14/2011 08:29 PM, Maas James Dr (MED) wrote:> Is there a way to convince R to create a character vector without using the quotes? > > This works > > ex1<- c("first","second") > > but when I try this it doesn't > > ext<- as.character(c(first,second)) > > it complains. I have many variables to put into character vectors so dispensing with the quotes would be useful. >Hi Jim, If you are reading values in from a file, you can get character vectors like this: <contents of file "charvec.txt"> Monday Tuesday Wednesday Thursday Friday Saturday charvec<-unlist(read.table("charvec.txt",stringsAsFactors=FALSE)) is.character(charvec) Jim
Jim, I don't know if this is the most elegant solution : Copy the following to your clipboard (some editors will require also to take last paragraph mark) first second third> ## now go to R and use > dat <- read.table("clipboard") # will give data.frame > datV1 1 first 2 second 3 third> ## or if you want a character-vector > dat <- as.character(as.matrix(read.table("clipboard"))) > dat[1] "first" "second" "third" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wolfgang Raffelsberger, PhD IGBMC, 1 rue Laurent Fries, 67404 Illkirch Strasbourg, France Tel (+33) 388 65 3300 Fax (+33) 388 65 3276 wolfgang.raffelsberger at igbmc.fr ________________________________________ De : r-help-bounces at r-project.org [r-help-bounces at r-project.org] de la part de Maas James Dr (MED) [J.Maas at uea.ac.uk] Date d'envoi : lundi 14 mars 2011 10:29 ? : r-help at r-project.org Objet : [R] creating character vector Is there a way to convince R to create a character vector without using the quotes? This works ex1 <- c("first","second") but when I try this it doesn't ext <- as.character(c(first,second)) it complains. I have many variables to put into character vectors so dispensing with the quotes would be useful. Thanks Jim ==============================Dr. Jim Maas University of East Anglia [[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.
You could try
scan(what=character(0), sep=",",
file=textConnection("first,second,third"))
but better to put the strings in a file (say, strings.txt), one per
line, and read it using
scan("strings.txt", what=character(0), sep="\n")
Even better is to understand what you are really trying to do and we can
maybe help with that.
Hope this helps a little
Allan
---
http://www.cybaea.net/Blogs/Data/
On 14/03/11 09:29, Maas James Dr (MED) wrote:> Is there a way to convince R to create a character vector without using the
quotes?
>
> This works
>
> ex1<- c("first","second")
>
> but when I try this it doesn't
>
> ext<- as.character(c(first,second))
>
> it complains. I have many variables to put into character vectors so
dispensing with the quotes would be useful.
>
> Thanks
>
> Jim
>
>
> ==============================> Dr. Jim Maas
> University of East Anglia
>
>
> [[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.
The reason this doesn't work is because R thinks that in the command as.character(c(first,second)) that first and second are variables that exist within the working environment. Since they don't (I assume), R doesn't know what to do with the command. Using the quotes indicates to R that you're specifying a character string type, instead of calling a variable. As other have said before, if you don't want to type in quotes, you could just read in a text file in the methods they describe. -- View this message in context: http://r.789695.n4.nabble.com/creating-character-vector-tp3353447p3354199.html Sent from the R help mailing list archive at Nabble.com.
Hi Jim create <-function(...) paste(substitute(list(...)))[-1] create(a,b,c) should work. hth. Am 14.03.2011 10:29, schrieb Maas James Dr (MED):> Is there a way to convince R to create a character vector without using the quotes? > > This works > > ex1 <- c("first","second") > > but when I try this it doesn't > > ext <- as.character(c(first,second)) > > it complains. I have many variables to put into character vectors so dispensing with the quotes would be useful. > > Thanks > > Jim > > > ==============================> Dr. Jim Maas > University of East Anglia > > > [[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.-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790
Hi Jim,
this may be barking up the wrong tree, but
create <-function(...) paste(substitute(list(...)))[-1]
createl <-function(...) {
tmp<-list(...)
names(tmp)<-create(...)
tmp
}
#eg
a<-1:4
b<-letters[2:6]
createl(a,b)
works. But I can't imagine that a named list is the one and only useful
data type for your problem.
Am 14.03.2011 19:36, schrieb Maas James Dr (MED):> Nice simple elegant solution .... care to have a crack at this one ...!!
>
> Thanks for help and answer!
>
> Jim
>
> In a parallel routine using foreach and doMPI have to put calculated values
into a list of lists such as
>
> list(one=one,
> two=two,
> three=three)
>
> It uses the same (large) list of variable names as in the previous
question. Is there a simpler way to accomplish this with a lapply statement?
>
>
> I've tried several permutations but no luck!
>
>
> ==============================> Dr. Jim Maas
> University of East Anglia
>
> From: Eik Vettorazzi [E.Vettorazzi at uke.uni-hamburg.de]
> Sent: 14 March 2011 18:07
> To: Maas James Dr (MED)
> Cc: r-help at r-project.org
> Subject: Re: [R] creating character vector
>
> Hi Jim
>
> create <-function(...) paste(substitute(list(...)))[-1]
>
> create(a,b,c)
>
> should work.
>
> hth.
>
> Am 14.03.2011 10:29, schrieb Maas James Dr (MED):
>> Is there a way to convince R to create a character vector without using
the quotes?
>>
>> This works
>>
>> ex1 <- c("first","second")
>>
>> but when I try this it doesn't
>>
>> ext <- as.character(c(first,second))
>>
>> it complains. I have many variables to put into character vectors so
dispensing with the quotes would be useful.
>>
>> Thanks
>>
>> Jim
>>
>>
>> ==============================>> Dr. Jim Maas
>> University of East Anglia
>>
>>
>> [[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.
>
> --
> Eik Vettorazzi
> Institut f?r Medizinische Biometrie und Epidemiologie
> Universit?tsklinikum Hamburg-Eppendorf
>
> Martinistr. 52
> 20246 Hamburg
>
> T ++49/40/7410-58243
> F ++49/40/7410-57790
--
Eik Vettorazzi
Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790
On Mar 14, 2011, at 16:32 , Allan Engelhardt wrote:> You could try > > scan(what=character(0), sep=",", file=textConnection("first,second,third")) > > > but better to put the strings in a file (say, strings.txt), one per line, and read it using > > scan("strings.txt", what=character(0), sep="\n")Also, things like this come in handy at times: ## From dept. of slightly dirty trix: txt <- "fmffmfmfmfmfmmmmfmmmffmff" lizards$sex <- factor(unlist(strsplit(txt,"")),levels=c("m","f")) It is easily generalized to include a delimiter. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com