Hi, Actually, you don't need to create the column first, nor to use which: DF[DF$month==1, "product1_1"] = DF[DF$month==1, "product1"] * 3.1 The "[" is a great tool that you need to learn. In this case, you don't need to combine "[" and $: within the square brackets, the vector before the comma indexes the rows and the one after the comma indexes the columns. The other thing you were missing correctly referencing the rows. You have to specify the data.frame you want to look into. And last, learn to use dput() to provide a nice reproducible example. HTH, Ivan -- Dr. Ivan Calandra TraCEr, Laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 23/05/2017 08:45, William Michels via R-help wrote:> Hi Lily, > > You're on the right track, but you should define a new column first > (filled with NA values), then specify the precise rows and columns on > both the left and right hand sides of the assignment operator that > will be altered. Luckily, this is pretty easy...just remember to use > which() on the right hand side of the assignment operator to get the > index of the rows you want. Example below for "product1": > >> DF$product1_1 <- NA >> DF[DF$month == 1, "product1_1"] <- DF[which(DF$month == 1), "product1"]*3.1 >> > > HTH, > > Bill. > > William Michels, Ph.D. > > > > > On Mon, May 22, 2017 at 9:56 PM, lily li <chocold12 at gmail.com> wrote: >> Hi R users, >> >> I have a question about manipulating the dataframe. I want to create a new >> dataframe, and to multiply rows with different seasons for different >> constants. >> >> DF >> year month day product1 product2 product3 >> 1981 1 1 18 56 20 >> 1981 1 2 19 45 22 >> 1981 1 3 16 48 28 >> 1981 1 4 19 50 21 >> 1981 2 1 17 49 25 >> 1981 2 2 20 47 23 >> 1981 2 3 21 52 27 >> >> For example, how to multiply product1 in month1 by 3.1, and to multiply >> product3 in month2 by 2.0? I wrote the code like this but does not work. >> Thanks for your help. >> >> DF['month'==1, ]$product1_1 = DF['month'==1, ]$product1 * 3.1; >> DF['month'==2, ]$product3_1 = DF['month'==1, ]$product3 * 2.0; >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. > ______________________________________________ > 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. >
Hi Ivan, I was just writing a follow-up note as your note came in. While the code I posted previously works fine, using which() is unnecessary.> DF <- read.csv("~/lily.csv") > DF$product1_1 <- NA > DF$product1_1 <- DF[DF$month == 1, "product1"]*3.1Error in `$<-.data.frame`(`*tmp*`, "product1_1", value = c(55.8, 58.9, : replacement has 4 rows, data has 7> DF$product1_1 <- DF[which(DF$month == 1), "product1"]*3.1Error in `$<-.data.frame`(`*tmp*`, "product1_1", value = c(55.8, 58.9, : replacement has 4 rows, data has 7> DF[DF$month == 1, "product1_1"] <- DF[DF$month == 1, "product1"]*3.1 > DFyear month day product1 product2 product3 product1_1 1 1981 1 1 18 56 20 55.8 2 1981 1 2 19 45 22 58.9 3 1981 1 3 16 48 28 49.6 4 1981 1 4 19 50 21 58.9 5 1981 2 1 17 49 25 NA 6 1981 2 2 20 47 23 NA 7 1981 2 3 21 52 27 NA>The two errors above were caused because I failed to specify rows on the left hand side of the assignment operator, not because I failed to use which(). Once rows and columns are specified on both sides, the assignment works fine. (I do however, prefer to create an "NA" column first. Personal preference ;-). Best Regards, Bill. On Mon, May 22, 2017 at 11:57 PM, Ivan Calandra <calandra at rgzm.de> wrote:> Hi, > > Actually, you don't need to create the column first, nor to use which: > DF[DF$month==1, "product1_1"] = DF[DF$month==1, "product1"] * 3.1 > > The "[" is a great tool that you need to learn. In this case, you don't need > to combine "[" and $: within the square brackets, the vector before the > comma indexes the rows and the one after the comma indexes the columns. > > The other thing you were missing correctly referencing the rows. You have to > specify the data.frame you want to look into. > > And last, learn to use dput() to provide a nice reproducible example. > > HTH, > Ivan > > > -- > Dr. Ivan Calandra > TraCEr, Laboratory for Traceology and Controlled Experiments > MONREPOS Archaeological Research Centre and > Museum for Human Behavioural Evolution > Schloss Monrepos > 56567 Neuwied, Germany > +49 (0) 2631 9772-243 > https://www.researchgate.net/profile/Ivan_Calandra > > On 23/05/2017 08:45, William Michels via R-help wrote: >> >> Hi Lily, >> >> You're on the right track, but you should define a new column first >> (filled with NA values), then specify the precise rows and columns on >> both the left and right hand sides of the assignment operator that >> will be altered. Luckily, this is pretty easy...just remember to use >> which() on the right hand side of the assignment operator to get the >> index of the rows you want. Example below for "product1": >> >>> DF$product1_1 <- NA >>> DF[DF$month == 1, "product1_1"] <- DF[which(DF$month == 1), >>> "product1"]*3.1 >>> >> >> HTH, >> >> Bill. >> >> William Michels, Ph.D. >> >> >> >> >> On Mon, May 22, 2017 at 9:56 PM, lily li <chocold12 at gmail.com> wrote: >>> >>> Hi R users, >>> >>> I have a question about manipulating the dataframe. I want to create a >>> new >>> dataframe, and to multiply rows with different seasons for different >>> constants. >>> >>> DF >>> year month day product1 product2 product3 >>> 1981 1 1 18 56 20 >>> 1981 1 2 19 45 22 >>> 1981 1 3 16 48 28 >>> 1981 1 4 19 50 21 >>> 1981 2 1 17 49 25 >>> 1981 2 2 20 47 23 >>> 1981 2 3 21 52 27 >>> >>> For example, how to multiply product1 in month1 by 3.1, and to multiply >>> product3 in month2 by 2.0? I wrote the code like this but does not work. >>> Thanks for your help. >>> >>> DF['month'==1, ]$product1_1 = DF['month'==1, ]$product1 * 3.1; >>> DF['month'==2, ]$product3_1 = DF['month'==1, ]$product3 * 2.0; >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> 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. >> >> ______________________________________________ >> 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. >> > > ______________________________________________ > 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.
Thanks for all your help. It works now. On Tue, May 23, 2017 at 1:34 AM, William Michels via R-help < r-help at r-project.org> wrote:> Hi Ivan, > > I was just writing a follow-up note as your note came in. While the > code I posted previously works fine, using which() is unnecessary. > > > DF <- read.csv("~/lily.csv") > > DF$product1_1 <- NA > > DF$product1_1 <- DF[DF$month == 1, "product1"]*3.1 > Error in `$<-.data.frame`(`*tmp*`, "product1_1", value = c(55.8, 58.9, : > replacement has 4 rows, data has 7 > > DF$product1_1 <- DF[which(DF$month == 1), "product1"]*3.1 > Error in `$<-.data.frame`(`*tmp*`, "product1_1", value = c(55.8, 58.9, : > replacement has 4 rows, data has 7 > > DF[DF$month == 1, "product1_1"] <- DF[DF$month == 1, "product1"]*3.1 > > DF > year month day product1 product2 product3 product1_1 > 1 1981 1 1 18 56 20 55.8 > 2 1981 1 2 19 45 22 58.9 > 3 1981 1 3 16 48 28 49.6 > 4 1981 1 4 19 50 21 58.9 > 5 1981 2 1 17 49 25 NA > 6 1981 2 2 20 47 23 NA > 7 1981 2 3 21 52 27 NA > > > > The two errors above were caused because I failed to specify rows on > the left hand side of the assignment operator, not because I failed to > use which(). Once rows and columns are specified on both sides, the > assignment works fine. > > (I do however, prefer to create an "NA" column first. Personal preference > ;-). > > Best Regards, > > Bill. > > > > On Mon, May 22, 2017 at 11:57 PM, Ivan Calandra <calandra at rgzm.de> wrote: > > Hi, > > > > Actually, you don't need to create the column first, nor to use which: > > DF[DF$month==1, "product1_1"] = DF[DF$month==1, "product1"] * 3.1 > > > > The "[" is a great tool that you need to learn. In this case, you don't > need > > to combine "[" and $: within the square brackets, the vector before the > > comma indexes the rows and the one after the comma indexes the columns. > > > > The other thing you were missing correctly referencing the rows. You > have to > > specify the data.frame you want to look into. > > > > And last, learn to use dput() to provide a nice reproducible example. > > > > HTH, > > Ivan > > > > > > -- > > Dr. Ivan Calandra > > TraCEr, Laboratory for Traceology and Controlled Experiments > > MONREPOS Archaeological Research Centre and > > Museum for Human Behavioural Evolution > > Schloss Monrepos > > 56567 Neuwied, Germany > > +49 (0) 2631 9772-243 > > https://www.researchgate.net/profile/Ivan_Calandra > > > > On 23/05/2017 08:45, William Michels via R-help wrote: > >> > >> Hi Lily, > >> > >> You're on the right track, but you should define a new column first > >> (filled with NA values), then specify the precise rows and columns on > >> both the left and right hand sides of the assignment operator that > >> will be altered. Luckily, this is pretty easy...just remember to use > >> which() on the right hand side of the assignment operator to get the > >> index of the rows you want. Example below for "product1": > >> > >>> DF$product1_1 <- NA > >>> DF[DF$month == 1, "product1_1"] <- DF[which(DF$month == 1), > >>> "product1"]*3.1 > >>> > >> > >> HTH, > >> > >> Bill. > >> > >> William Michels, Ph.D. > >> > >> > >> > >> > >> On Mon, May 22, 2017 at 9:56 PM, lily li <chocold12 at gmail.com> wrote: > >>> > >>> Hi R users, > >>> > >>> I have a question about manipulating the dataframe. I want to create a > >>> new > >>> dataframe, and to multiply rows with different seasons for different > >>> constants. > >>> > >>> DF > >>> year month day product1 product2 product3 > >>> 1981 1 1 18 56 20 > >>> 1981 1 2 19 45 22 > >>> 1981 1 3 16 48 28 > >>> 1981 1 4 19 50 21 > >>> 1981 2 1 17 49 25 > >>> 1981 2 2 20 47 23 > >>> 1981 2 3 21 52 27 > >>> > >>> For example, how to multiply product1 in month1 by 3.1, and to multiply > >>> product3 in month2 by 2.0? I wrote the code like this but does not > work. > >>> Thanks for your help. > >>> > >>> DF['month'==1, ]$product1_1 = DF['month'==1, ]$product1 * 3.1; > >>> DF['month'==2, ]$product3_1 = DF['month'==1, ]$product3 * 2.0; > >>> > >>> [[alternative HTML version deleted]] > >>> > >>> ______________________________________________ > >>> 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. > >> > >> ______________________________________________ > >> 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. > >> > > > > ______________________________________________ > > 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. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]