jeff6868
2012-Sep-26 10:49 UTC
[R] create new column in a DF according to values from another column
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 automatically filled with: "V1" or "V2" or
"V3".
The choice must be done on the numbers (1st column).
For example, I would like to have "V1" in the column
"Station" in the rows
where the numbers of the 1st column are: 1,7,11,16 ; then I would like to
have "V2" in the rows where the numbers are: 4,14,20 and finally
"V3" in the
rows where the numbers are: 3,17,19.
I'm trying with "if" and something like this, but it's not
working yet:
# For "V1":
if(DF$number %in% c(1,7,11,16)) {test$Station=="V1"}
# For "V2":
...
So my final DF should look like this:
FINALDF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10),
Station=c("V1","V2","V1","V3","V1","V1","V2","V3","V2","V3"))
Could someone help me to finish this?
Thank you very much!!!
--
View this message in context:
http://r.789695.n4.nabble.com/create-new-column-in-a-DF-according-to-values-from-another-column-tp4644217.html
Sent from the R help mailing list archive at Nabble.com.
Berend Hasselman
2012-Sep-26 11:17 UTC
[R] create new column in a DF according to values from another column
On 26-09-2012, at 12:49, jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> 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 automatically filled with: "V1" or "V2" or "V3". > The choice must be done on the numbers (1st column). > > For example, I would like to have "V1" in the column "Station" in the rows > where the numbers of the 1st column are: 1,7,11,16 ; then I would like to > have "V2" in the rows where the numbers are: 4,14,20 and finally "V3" in the > rows where the numbers are: 3,17,19. > > I'm trying with "if" and something like this, but it's not working yet: > # For "V1": > if(DF$number %in% c(1,7,11,16)) {test$Station=="V1"} > # For "V2": > ... > > So my final DF should look like this: > > FINALDF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10), > Station=c("V1","V2","V1","V3","V1","V1","V2","V3","V2","V3")) >DF[DF$number %in% c(1,7,11,16),"Station"] <- "V1" DF[DF$number %in% c(4,14,20),"Station"] <- "V2" DF[DF$number %in% c(3,17,19),"Station"] <- "V3" DF The Station column is of type character. To make FINALDF identical you should add stringsAsFactors=FALSE to the arguments of data.frame. Berend
Rui Barradas
2012-Sep-26 11:26 UTC
[R] create new column in a DF according to values from another column
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$number %in% v1, "V1",
ifelse(DF$number %in% v2, "V2", "V3"))
Hope this helps,
Rui Barradas
Em 26-09-2012 11:49, jeff6868 escreveu:> 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 automatically filled with: "V1" or "V2" or
"V3".
> The choice must be done on the numbers (1st column).
>
> For example, I would like to have "V1" in the column
"Station" in the rows
> where the numbers of the 1st column are: 1,7,11,16 ; then I would like to
> have "V2" in the rows where the numbers are: 4,14,20 and finally
"V3" in the
> rows where the numbers are: 3,17,19.
>
> I'm trying with "if" and something like this, but it's
not working yet:
> # For "V1":
> if(DF$number %in% c(1,7,11,16)) {test$Station=="V1"}
> # For "V2":
> ...
>
> So my final DF should look like this:
>
> FINALDF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10),
>
Station=c("V1","V2","V1","V3","V1","V1","V2","V3","V2","V3"))
>
> Could someone help me to finish this?
>
> Thank you very much!!!
>
>
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/create-new-column-in-a-DF-according-to-values-from-another-column-tp4644217.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
jeff6868
2012-Sep-26 11:34 UTC
[R] create new column in a DF according to values from another column
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.
jim holtman
2012-Sep-26 12:09 UTC
[R] create new column in a DF according to values from another column
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 <- list(list(c(1, 7, 11, 16), "V1")+ , list(c(4, 14, 20), "V2") + , list(c(3, 17, 19), "V3") + )> for (i in replace){+ DF$Station[DF$number %in% i[[1L]]] <- i[[2L]] + }> DFnumber data Station 1 1 1 V1 2 4 2 V2 3 7 3 V1 4 3 4 V3 5 11 5 V1 6 16 6 V1 7 14 7 V2 8 17 8 V3 9 20 9 V2 10 19 10 V3 On Wed, Sep 26, 2012 at 6:49 AM, jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> 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 automatically filled with: "V1" or "V2" or "V3". > The choice must be done on the numbers (1st column). > > For example, I would like to have "V1" in the column "Station" in the rows > where the numbers of the 1st column are: 1,7,11,16 ; then I would like to > have "V2" in the rows where the numbers are: 4,14,20 and finally "V3" in the > rows where the numbers are: 3,17,19. > > I'm trying with "if" and something like this, but it's not working yet: > # For "V1": > if(DF$number %in% c(1,7,11,16)) {test$Station=="V1"} > # For "V2": > ... > > So my final DF should look like this: > > FINALDF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10), > Station=c("V1","V2","V1","V3","V1","V1","V2","V3","V2","V3")) > > Could someone help me to finish this? > > Thank you very much!!! > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/create-new-column-in-a-DF-according-to-values-from-another-column-tp4644217.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
arun
2012-Sep-26 12:43 UTC
[R] create new column in a DF according to values from another column
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????? 16??? 6????? V1
#7????? 14??? 7????? V2
#8????? 17??? 8????? V3
#9????? 20??? 9????? V2
#10???? 19?? 10????? V3
#or if you have more replacement values, then:
library(car)
DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10))
DF1<-DF1
DF1$Station<-recode(DF1$number,"c(1,7,11,16)='V1';c(4,14,20)='V2';c(3,17,19)='V3'")
?DF1
#?? number data Station
#1?????? 1??? 1????? V1
#2?????? 4??? 2????? V2
#3?????? 7??? 3????? V1
#4?????? 3??? 4????? V3
#5????? 11??? 5????? V1
#6????? 16??? 6????? V1
#7????? 14??? 7????? V2
#8????? 17??? 8????? V3
#9????? 20??? 9????? V2
#10???? 19?? 10????? V3
A.K.
----- Original Message -----
From: jeff6868 <geoffrey_klein at etu.u-bourgogne.fr>
To: r-help at r-project.org
Cc:
Sent: Wednesday, September 26, 2012 6:49 AM
Subject: [R] create new column in a DF according to values from another column
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 automatically filled with: "V1" or "V2" or
"V3".
The choice must be done on the numbers (1st column).
For example, I would like to have "V1" in the column
"Station" in the rows
where the numbers of the 1st column are: 1,7,11,16 ; then I would like to
have "V2" in the rows where the numbers are: 4,14,20 and finally
"V3" in the
rows where the numbers are: 3,17,19.
I'm trying with "if" and something like this, but it's not
working yet:
# For "V1":
if(DF$number %in% c(1,7,11,16)) {test$Station=="V1"}
# For "V2":
...
So my final DF should look like this:
FINALDF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10),
Station=c("V1","V2","V1","V3","V1","V1","V2","V3","V2","V3"))
Could someone help me to finish this?
Thank you very much!!!
--
View this message in context:
http://r.789695.n4.nabble.com/create-new-column-in-a-DF-according-to-values-from-another-column-tp4644217.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
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.