[R] Help with vectors!

Eik Vettorazzi E.Vettorazzi at uke.de
Wed Sep 9 13:31:23 CEST 2015


how about this:

match(VAS,unique(VAS))

#or, preserving given order

match(VAS,c("White","Yellow","Green","Black"))

Cheers.

Am 05.09.2015 um 23:14 schrieb Dan D:
> # your data
> VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow","Black","Green","Black")
> 
> # declare the new vector
> New_Vector<-numeric(length(VAS))
> 
> # brute force:
> New_Vector[VAS=="White"]<-1
> New_Vector[VAS=="Yellow"]<-2
> New_Vector[VAS=="Green"]<-3
> New_Vector[VAS=="Black"]<-4
> 
> # a little more subtle
> cols<-c("White","Yellow","Green","Black")
> for (i in 1:length(cols))  New_Vector[VAS==cols[i]]<-i
> 
> # and a general approach (that may give a different indexing, but can be
> used for any array)
> for (i in 1:length(unique(VAS))) New_Vector[VAS==unique(VAS)[i]]<-i
> cbind(1:length(unique(VAS)),unique(VAS)) # a decoding key for the color
> index
> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-with-vectors-tp4711801p4711895.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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

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

_____________________________________________________________________

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Prölß, Rainer Schoppik
_____________________________________________________________________

SAVE PAPER - THINK BEFORE PRINTING



More information about the R-help mailing list