Or, you may use this approach:

> attach(achtergrond)
> spits <- ifelse(uurenminuut >= 5.30 & uurenminuut < 9.30, "morning",
+ ifelse(uurenminuut >=16.30 & uurenminuut < 19.0, "evening",
+ "between"))
> table(spits)
spits
between evening morning
   1636     142     579
>

I personally like the approach presented by Bill Dunlap (in the previous message). I think his approach is smart and nice. You will see the same results as shown in the above:

> achtergrond$spits=cut(achtergrond$uurenminuut,c(-1.0,5.30,9.30,16.30,19.0,24.0),right=FALSE) > levels(achtergrond$spits) <- c("between","morning","between","evening","between")
> table(achtergrond$spits)

between morning evening
   1636     579     142
>

You can also use function 'findInterval()' instead of using 'cut()'. I hope this helps.

Chel Hee Lee


On 12/02/2014 02:04 PM, William Dunlap wrote:
You can do this in 2 steps - have cut() make a factor with a different
level for each time period
then use levels<-() to merge some of the levels.
    > z <- cut(.5:3.5, breaks=c(0,1,2,3,4), labels=c("0-1", "1-2", "2-3",
"3-4"))
    > levels(z)
    [1] "0-1" "1-2" "2-3" "3-4"
    > levels(z) <- c("between", "1-2", "between", "3-4") # or
levels(z)[c(1,3)] <- "between"
    > str(z)
    Factor w/ 3 levels "between","1-2",..: 1 2 1 3




Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Dec 2, 2014 at 11:32 AM, Dries David <dries-da...@hotmail.com>
wrote:


Hey
I have a question about making a new variable in R. I have put my
dataset in attachment. I have to make a new variable "spits" where
spits=morning when uurenminuut (also a variabel) is between 5.30 and 9.30,
when uurenminuut is between 16.30 and 19.0 spits has to be equal to
evening. But here is my problem: for all the values not between 5.30- 9.30
and 16.30-19.0 spits must be equal to "between"

achtergrond$minuutdec=achtergrond$minuut/100
achtergrond$uurenminuut=achtergrond$uur+achtergrond$minuutdec

achtergrond$spits=cut(uurenminuut,c(-1.0,5.30,9.30,16.30,19.0,24.0),labels=c("between","morning","between","evening","between"),right=FALSE)

When I do this i get a warning message, because I use between more
than once as label. Between has to be one label that covers all values that
are not in morning and evening.

Could you help me with this?

Kind regards

Dries David




______________________________________________
R-help@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.



        [[alternative HTML version deleted]]

______________________________________________
R-help@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.


______________________________________________
R-help@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.

Reply via email to