Hi everyone, A simple question, but i cannot figure this out. I have a data-frame with 4 columns (onset, offset, outcome, mean): onset offset outcome mean8 72071 72503 1 7244615 142598 143030 1 NaN30 293729 294161 1 294080 For each 'NaN' in the mean column, i want to replace that NaN with the 'offset' value in the same row. Intended outcome: onset offset outcome mean8 72071 72503 1 7244615 142598 143030 1 14303030 293729 294161 1 294080 I have tried: df$mean <- replace(df$mean, is.na(df$mean), df$offset) but i get the error message: 'number of items to replace is not a multiple of replacement length'. I'm assuming because this is trying to insert the whole 'offset' column into my one NaN cell. Is this a correct interpretation of the error message? Can anyone tell me how to replace any mean row NaN's with the offset value from that very same row? I don't want to use any pasting etc as this needs to be used as part of a function working over a large dataset than the one shown here. Cheers Jonathan [[alternative HTML version deleted]]
Next time send your email using plain text format rather than HTML so we see what you saw. Try idx <- is.na( df$mean ) df[ idx, "mean" ] <- df[ idx, "offset" ] BTW there is a commonly-used function called df, so you might improve clarity by using DF for your temporary data frame name. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On October 18, 2015 10:48:14 AM PDT, Jonathan Reardon <jonathanreardon at outlook.com> wrote:>Hi everyone, >A simple question, but i cannot figure this out. > >I have a data-frame with 4 columns (onset, offset, outcome, mean): >onset offset outcome mean8 72071 72503 1 7244615 142598 >143030 1 NaN30 293729 294161 1 294080 >For each 'NaN' in the mean column, i want to replace that NaN with the >'offset' value in the same row. >Intended outcome: >onset offset outcome mean8 72071 72503 1 7244615 142598 >143030 1 14303030 293729 294161 1 294080 >I have tried: > df$mean <- replace(df$mean, is.na(df$mean), df$offset) >but i get the error message: 'number of items to replace is not a >multiple of replacement length'. I'm assuming because this is trying to >insert the whole 'offset' column into my one NaN cell. Is this a >correct interpretation of the error message? >Can anyone tell me how to replace any mean row NaN's with the offset >value from that very same row? >I don't want to use any pasting etc as this needs to be used as part of >a function working over a large dataset than the one shown here. >Cheers >Jonathan > [[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.
Please reply to all so the answers are seen on the mailing list, where my mistakes can be corrected and others can learn from any valid answers I manage to provide. I warned you that df is the name of a common function. I used the name of the variable that you referred to in your question, which had hidden the common function called df. I would guess that you have not re-created the variables in R as you had them defined before. To keep your mind clear on what R is thinking, learn to use the ls and str functions. Read about them using the help system: ?ls ?str I think that str(df) tells you that df is a function, rather than a data.frame. If you create a variable called df like so: df <- data.frame( test=5 ) or using any other function that gives you a data frame such as read.csv, then the df function in the base stats package will be hidden and str(df) will give you a different result. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On October 18, 2015 11:29:51 AM PDT, Jonathan Reardon <jonathanreardon at outlook.com> wrote:>How do i send an email in plain text format and not HTML? >I tried: >idx <- is.na( df$mean ) >df[ idx, "mean" ] <- df[ idx, "offset" ] >I got the error message: >In is.na(df$mean) : is.na() applied to non-(list or vector) of type >'NULL' >Jon > >> Subject: Re: [R] Replace NaN with value from the same row >> From: jdnewmil at dcn.davis.CA.us >> Date: Sun, 18 Oct 2015 11:06:44 -0700 >> To: jonathanreardon at outlook.com; r-help at r-project.org >> >> Next time send your email using plain text format rather than HTML so >we see what you saw. >> >> Try >> >> idx <- is.na( df$mean ) >> df[ idx, "mean" ] <- df[ idx, "offset" ] >> >> BTW there is a commonly-used function called df, so you might improve >clarity by using DF for your temporary data frame name. >> >--------------------------------------------------------------------------- >> Jeff Newmiller The ..... ..... Go >Live... >> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live >Go... >> Live: OO#.. Dead: OO#.. >Playing >> Research Engineer (Solar/Batteries O.O#. #.O#. with >> /Software/Embedded Controllers) .OO#. .OO#. >rocks...1k >> >--------------------------------------------------------------------------- > >> Sent from my phone. Please excuse my brevity. >> >> On October 18, 2015 10:48:14 AM PDT, Jonathan Reardon ><jonathanreardon at outlook.com> wrote: >> >Hi everyone, >> >A simple question, but i cannot figure this out. >> > >> >I have a data-frame with 4 columns (onset, offset, outcome, mean): >> >onset offset outcome mean8 72071 72503 1 7244615 142598 >> >143030 1 NaN30 293729 294161 1 294080 >> >For each 'NaN' in the mean column, i want to replace that NaN with >the >> >'offset' value in the same row. >> >Intended outcome: >> >onset offset outcome mean8 72071 72503 1 7244615 142598 >> >143030 1 14303030 293729 294161 1 294080 >> >I have tried: >> > df$mean <- replace(df$mean, is.na(df$mean), df$offset) >> >but i get the error message: 'number of items to replace is not a >> >multiple of replacement length'. I'm assuming because this is trying >to >> >insert the whole 'offset' column into my one NaN cell. Is this a >> >correct interpretation of the error message? >> >Can anyone tell me how to replace any mean row NaN's with the >offset >> >value from that very same row? >> >I don't want to use any pasting etc as this needs to be used as part >of >> >a function working over a large dataset than the one shown here. >> >Cheers >> >Jonathan >> > [[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. >> >
You should (re-)read the intro document that comes with R, "An Introduction to R". Pay particular attention to sections 2., 2.7, and 5.2. The "idx" variable that I defined is a vector in the current environment (in your case apparently a local function environment). It is not a column in your data frame. You should look at it using the str function. (You might need to print the result of str, or use the debug capability of R to single-step through your function and then use str. Read the help at ?debug.) The df[ idx, "offset" ] notation uses the logical indexing and string indexing concepts in section 2.7 to select a subset of the rows and one column of the data frame. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On October 18, 2015 12:24:42 PM PDT, Jonathan Reardon <jonathanreardon at outlook.com> wrote:>Hi, Sorry to be a pain. Would you be kind enough to briefly explain >what the lines are doing? >From what i can gather, 'idx <- is.na( df$mean )' is making a new >column called 'idx', finds the NaN values and inserts the boolean TRUE >in the respective cell. >df[ idx, "mean" ] <- df[ idx, "offset" ] << i am unsure what this >is doing exactly. >Jon > > >> Subject: RE: [R] Replace NaN with value from the same row >> From: jdnewmil at dcn.davis.CA.us >> Date: Sun, 18 Oct 2015 12:09:02 -0700 >> To: jonathanreardon at outlook.com >> >> The Posting Guide mentioned at the bottom of every email in the list >tells you that such an option is in your email software, which I know >nothing about. Most software lets you choose the format as part of >composing each email, but some software will let you set a default >format to use for each email address (so all your emails to e.g. >r-help at r-project.org will be plain text). >> >--------------------------------------------------------------------------- >> Jeff Newmiller The ..... ..... Go >Live... >> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live >Go... >> Live: OO#.. Dead: OO#.. >Playing >> Research Engineer (Solar/Batteries O.O#. #.O#. with >> /Software/Embedded Controllers) .OO#. .OO#. >rocks...1k >> >--------------------------------------------------------------------------- > >> Sent from my phone. Please excuse my brevity. >> >> On October 18, 2015 11:29:51 AM PDT, Jonathan Reardon ><jonathanreardon at outlook.com> wrote: >> >How do i send an email in plain text format and not HTML? >> >I tried: >> >idx <- is.na( df$mean ) >> >df[ idx, "mean" ] <- df[ idx, "offset" ] >> >I got the error message: >> >In is.na(df$mean) : is.na() applied to non-(list or vector) of type >> >'NULL' >> >Jon >> > >> >> Subject: Re: [R] Replace NaN with value from the same row >> >> From: jdnewmil at dcn.davis.CA.us >> >> Date: Sun, 18 Oct 2015 11:06:44 -0700 >> >> To: jonathanreardon at outlook.com; r-help at r-project.org >> >> >> >> Next time send your email using plain text format rather than HTML >so >> >we see what you saw. >> >> >> >> Try >> >> >> >> idx <- is.na( df$mean ) >> >> df[ idx, "mean" ] <- df[ idx, "offset" ] >> >> >> >> BTW there is a commonly-used function called df, so you might >improve >> >clarity by using DF for your temporary data frame name. >> >> >> >>--------------------------------------------------------------------------- >> >> Jeff Newmiller The ..... ..... >Go >> >Live... >> >> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. >Live >> >Go... >> >> Live: OO#.. Dead: OO#.. >> >Playing >> >> Research Engineer (Solar/Batteries O.O#. #.O#. >with >> >> /Software/Embedded Controllers) .OO#. .OO#. >> >rocks...1k >> >> >> >>--------------------------------------------------------------------------- >> > >> >> Sent from my phone. Please excuse my brevity. >> >> >> >> On October 18, 2015 10:48:14 AM PDT, Jonathan Reardon >> ><jonathanreardon at outlook.com> wrote: >> >> >Hi everyone, >> >> >A simple question, but i cannot figure this out. >> >> > >> >> >I have a data-frame with 4 columns (onset, offset, outcome, >mean): >> >> >onset offset outcome mean8 72071 72503 1 7244615 >142598 >> >> >143030 1 NaN30 293729 294161 1 294080 >> >> >For each 'NaN' in the mean column, i want to replace that NaN >with >> >the >> >> >'offset' value in the same row. >> >> >Intended outcome: >> >> >onset offset outcome mean8 72071 72503 1 7244615 >142598 >> >> >143030 1 14303030 293729 294161 1 294080 >> >> >I have tried: >> >> > df$mean <- replace(df$mean, is.na(df$mean), df$offset) >> >> >but i get the error message: 'number of items to replace is not a >> >> >multiple of replacement length'. I'm assuming because this is >trying >> >to >> >> >insert the whole 'offset' column into my one NaN cell. Is this a >> >> >correct interpretation of the error message? >> >> >Can anyone tell me how to replace any mean row NaN's with the >> >offset >> >> >value from that very same row? >> >> >I don't want to use any pasting etc as this needs to be used as >part >> >of >> >> >a function working over a large dataset than the one shown here. >> >> >Cheers >> >> >Jonathan >> >> > [[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. >> >> >> > >> >
Ok, i will do, thanks for your help. J> Subject: RE: [R] Replace NaN with value from the same row > From: jdnewmil at dcn.davis.CA.us > Date: Sun, 18 Oct 2015 12:55:14 -0700 > To: jonathanreardon at outlook.com > CC: r-help at r-project.org > > You should (re-)read the intro document that comes with R, "An Introduction to R". Pay particular attention to sections 2., 2.7, and 5.2. > > The "idx" variable that I defined is a vector in the current environment (in your case apparently a local function environment). It is not a column in your data frame. You should look at it using the str function. (You might need to print the result of str, or use the debug capability of R to single-step through your function and then use str. Read the help at ?debug.) > > The df[ idx, "offset" ] notation uses the logical indexing and string indexing concepts in section 2.7 to select a subset of the rows and one column of the data frame. > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > On October 18, 2015 12:24:42 PM PDT, Jonathan Reardon <jonathanreardon at outlook.com> wrote: > >Hi, Sorry to be a pain. Would you be kind enough to briefly explain > >what the lines are doing? > >From what i can gather, 'idx <- is.na( df$mean )' is making a new > >column called 'idx', finds the NaN values and inserts the boolean TRUE > >in the respective cell. > >df[ idx, "mean" ] <- df[ idx, "offset" ] << i am unsure what this > >is doing exactly. > >Jon > > > > > >> Subject: RE: [R] Replace NaN with value from the same row > >> From: jdnewmil at dcn.davis.CA.us > >> Date: Sun, 18 Oct 2015 12:09:02 -0700 > >> To: jonathanreardon at outlook.com > >> > >> The Posting Guide mentioned at the bottom of every email in the list > >tells you that such an option is in your email software, which I know > >nothing about. Most software lets you choose the format as part of > >composing each email, but some software will let you set a default > >format to use for each email address (so all your emails to e.g. > >r-help at r-project.org will be plain text). > >> > >--------------------------------------------------------------------------- > >> Jeff Newmiller The ..... ..... Go > >Live... > >> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live > >Go... > >> Live: OO#.. Dead: OO#.. > >Playing > >> Research Engineer (Solar/Batteries O.O#. #.O#. with > >> /Software/Embedded Controllers) .OO#. .OO#. > >rocks...1k > >> > >--------------------------------------------------------------------------- > > > >> Sent from my phone. Please excuse my brevity. > >> > >> On October 18, 2015 11:29:51 AM PDT, Jonathan Reardon > ><jonathanreardon at outlook.com> wrote: > >> >How do i send an email in plain text format and not HTML? > >> >I tried: > >> >idx <- is.na( df$mean ) > >> >df[ idx, "mean" ] <- df[ idx, "offset" ] > >> >I got the error message: > >> >In is.na(df$mean) : is.na() applied to non-(list or vector) of type > >> >'NULL' > >> >Jon > >> > > >> >> Subject: Re: [R] Replace NaN with value from the same row > >> >> From: jdnewmil at dcn.davis.CA.us > >> >> Date: Sun, 18 Oct 2015 11:06:44 -0700 > >> >> To: jonathanreardon at outlook.com; r-help at r-project.org > >> >> > >> >> Next time send your email using plain text format rather than HTML > >so > >> >we see what you saw. > >> >> > >> >> Try > >> >> > >> >> idx <- is.na( df$mean ) > >> >> df[ idx, "mean" ] <- df[ idx, "offset" ] > >> >> > >> >> BTW there is a commonly-used function called df, so you might > >improve > >> >clarity by using DF for your temporary data frame name. > >> >> > >> > >>--------------------------------------------------------------------------- > >> >> Jeff Newmiller The ..... ..... > >Go > >> >Live... > >> >> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. > >Live > >> >Go... > >> >> Live: OO#.. Dead: OO#.. > >> >Playing > >> >> Research Engineer (Solar/Batteries O.O#. #.O#. > >with > >> >> /Software/Embedded Controllers) .OO#. .OO#. > >> >rocks...1k > >> >> > >> > >>--------------------------------------------------------------------------- > >> > > >> >> Sent from my phone. Please excuse my brevity. > >> >> > >> >> On October 18, 2015 10:48:14 AM PDT, Jonathan Reardon > >> ><jonathanreardon at outlook.com> wrote: > >> >> >Hi everyone, > >> >> >A simple question, but i cannot figure this out. > >> >> > > >> >> >I have a data-frame with 4 columns (onset, offset, outcome, > >mean): > >> >> >onset offset outcome mean8 72071 72503 1 7244615 > >142598 > >> >> >143030 1 NaN30 293729 294161 1 294080 > >> >> >For each 'NaN' in the mean column, i want to replace that NaN > >with > >> >the > >> >> >'offset' value in the same row. > >> >> >Intended outcome: > >> >> >onset offset outcome mean8 72071 72503 1 7244615 > >142598 > >> >> >143030 1 14303030 293729 294161 1 294080 > >> >> >I have tried: > >> >> > df$mean <- replace(df$mean, is.na(df$mean), df$offset) > >> >> >but i get the error message: 'number of items to replace is not a > >> >> >multiple of replacement length'. I'm assuming because this is > >trying > >> >to > >> >> >insert the whole 'offset' column into my one NaN cell. Is this a > >> >> >correct interpretation of the error message? > >> >> >Can anyone tell me how to replace any mean row NaN's with the > >> >offset > >> >> >value from that very same row? > >> >> >I don't want to use any pasting etc as this needs to be used as > >part > >> >of > >> >> >a function working over a large dataset than the one shown here. > >> >> >Cheers > >> >> >Jonathan > >> >> > [[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. > >> >> > >> > > >> > > >[[alternative HTML version deleted]]