HI,
Try this:
DF$Station[DF$number%in%c(1,7,11,16)]<-"V1"
DF$Station[DF$number%in%c(4,14,20)]<-"V2"
DF$Station[DF$number%in%c(3,17,19)]<-"V3"
DF
# number data Station
#1 1 1 V1
#2 4 2 V2
#3 7 3 V1
#4 3 4 V3
#5 11 5 V1
#6
Hello,
'if' is not vectorized, it only uses the first value of the multiple
condition. 'ifelse' is vectorized and in your case use nested ifelses.
DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10))
v1 <- c(1,7,11,16)
v2 <- c(4,14,20)
v3 <- c(3,17,19)
DF$Station <- ifelse(DF$n
Yes this is it!
Thank you for your help Berend!
--
View this message in context:
http://r.789695.n4.nabble.com/create-new-column-in-a-DF-according-to-values-from-another-column-tp4644217p4644225.html
Sent from the R help mailing list archive at Nabble.com.
Here is another technique to use, especially if you have a long list
of replacement values:
> DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10))
>
> # create a list of replacement values; if you have a lot and
> # you can create them automagically, then it is easier
> replace <- l
On 26-09-2012, at 12:49, jeff6868 wrote:
> Hi everyone,
>
> I have a small problem in my R-code.
>
> Imagine this DF for example:
>
> DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10))
>
> I would like to add a new column "Station" in this DF. This new column must
> be automa
5 matches
Mail list logo