Help with vectors!

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with vectors!

BreakingtheLaw
This post has NOT been accepted by the mailing list yet.
Dear users,

I need your help!
I want to create a vector which is dependent on another vector. In particular, I have the vector VAS with 4 colours:

White
Yellow
Green
Black

and I want to create a vector(New_Vector) whose values are dependent on VAS in the following way:

if the first element of VAS is = "White", the corresponding element of New_Vector is "1", if VAS=Yellow, New_Vector=2, if VAS=Green, New_Vector=3, etc.



VAS              New_Vector
Green           3
Green           3
Black            4
Green           3
White            1
Yellow           2
Yellow           2
Black            4
Green           3
Black            4

Any suggestions?

Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

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
Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

Frank Schwidom
On Sat, Sep 05, 2015 at 02:14:18PM -0700, Dan D wrote:

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

# how about:

rank( VAS, ties.method='min')

Regards

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

Dan D
Great!
Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

Frank Schwidom
# my last one:

xtfrm( VAS)

On Tue, Sep 08, 2015 at 11:55:51AM -0700, Dan D wrote:

> Great!
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-with-vectors-tp4711801p4712023.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

Eik Vettorazzi-2
In reply to this post by Dan D
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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

Frank Schwidom
In reply to this post by Dan D

c( as.factor( VAS))

On Sat, Sep 05, 2015 at 02:14:18PM -0700, Dan D wrote:

> # 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

Dan D
VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow","Black","Green","Black")
c(factor(VAS)) # to give integer indexing to the colors

---
This is very nice, Frank. And it can be easily adjusted to match the original criterion that the numbers match the order of appearance of the colors in the original vector:

c(factor(VAS,levels=unique(VAS)))
Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

Frank Schwidom
In reply to this post by Dan D

Just for fun:

> colSums( outer( VAS, VAS, '<'))
 [1] 3 3 0 3 7 8 8 0 3 0


On Sat, Sep 05, 2015 at 02:14:18PM -0700, Dan D wrote:

> # 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with vectors!

Rolf Turner
In reply to this post by Frank Schwidom
On 10/09/15 06:30, Frank Schwidom wrote:

> c( as.factor( VAS))
>
> On Sat, Sep 05, 2015 at 02:14:18PM -0700, Dan D wrote:
>> # your data
>> VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow",
>>        "Black","Green","Black")

<SNIP>

Better:

     as.numeric(factor(VAS))


See fortune(185).

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.