Christophe Elek
2017-Jun-24 17:49 UTC
[R] Fill in empty cell in data.frame from previous value
Hello Total newbie here... I hope I read the guide properly I have the following data.frame (I read it from a CSV file I cannot change) names val 1 Mandy 1 2 2 3 John 2 4 2 I want to read the row number 2, but I want the first column to be ?Mandy? and not null print (frame[2,]) 2 Mandy 2 I can manipulate the data.frame once loaded How can I fill all cell in column ?names? with the previous value ? Or is there a function that will get me the row and fill the ?names? column ? NOTA BENE: I do not want the answer, I want to find it myself but I need guidance If there is a function, tell me the library and I will search If this is an algorithm, tell me generally how you would do and let me scratch my head first ? Thanks Chris [[alternative HTML version deleted]]
Hi Chris, You may know about the *apply family of functions. These slice various data structures and "apply" a specified function to each slice, usually returning a list of return values. As far as I am aware, you can't access adjacent rows unless you reformat the data structure. There is a way to do this particular job. It requires the sequence operator (:), the ifelse function and indexing. What you do is to create a sequence of all the values in the element "names", then subtract 1 for all the NA (or any other specified value) values and use the resulting vector to index the original element "names". It won't work if the first value is NA, nor will it work for more than one NA in a row. I realize that this is pretty obscure, but you said that you didn't just want the solution. It's a one-liner. Jim On Sun, Jun 25, 2017 at 3:49 AM, Christophe Elek <christophe.elek at gmail.com> wrote:> Hello Total newbie here... I hope I read the guide properly > > I have the following data.frame (I read it from a CSV file I cannot change) > > names val > 1 Mandy 1 > 2 2 > 3 John 2 > 4 2 > > I want to read the row number 2, but I want the first column to be ?Mandy? and not null > > print (frame[2,]) > 2 Mandy 2 > > I can manipulate the data.frame once loaded > How can I fill all cell in column ?names? with the previous value ? > Or is there a function that will get me the row and fill the ?names? column ? > > NOTA BENE: I do not want the answer, I want to find it myself but I need guidance > If there is a function, tell me the library and I will search > If this is an algorithm, tell me generally how you would do and let me scratch my head first ? > > Thanks Chris > > > [[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.
Bert Gunter
2017-Jun-25 14:59 UTC
[R] Fill in empty cell in data.frame from previous value
You need to go through some tutorials, not expect us to tutor you here. That is not the purpose of this list (although we do some of this indirectly of course). There are many good online tutorials -- search or see here: https://www.rstudio.com/online-learning/#R . Or start by going through the Intro to R tutorial that ships with R. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Jun 25, 2017 at 12:11 AM, Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Chris, > You may know about the *apply family of functions. These slice various > data structures and "apply" a specified function to each slice, > usually returning a list of return values. As far as I am aware, you > can't access adjacent rows unless you reformat the data structure. > > There is a way to do this particular job. It requires the sequence > operator (:), the ifelse function and indexing. What you do is to > create a sequence of all the values in the element "names", then > subtract 1 for all the NA (or any other specified value) values and > use the resulting vector to index the original element "names". It > won't work if the first value is NA, nor will it work for more than > one NA in a row. I realize that this is pretty obscure, but you said > that you didn't just want the solution. It's a one-liner. > > Jim > > On Sun, Jun 25, 2017 at 3:49 AM, Christophe Elek > <christophe.elek at gmail.com> wrote: >> Hello Total newbie here... I hope I read the guide properly >> >> I have the following data.frame (I read it from a CSV file I cannot change) >> >> names val >> 1 Mandy 1 >> 2 2 >> 3 John 2 >> 4 2 >> >> I want to read the row number 2, but I want the first column to be ?Mandy? and not null >> >> print (frame[2,]) >> 2 Mandy 2 >> >> I can manipulate the data.frame once loaded >> How can I fill all cell in column ?names? with the previous value ? >> Or is there a function that will get me the row and fill the ?names? column ? >> >> NOTA BENE: I do not want the answer, I want to find it myself but I need guidance >> If there is a function, tell me the library and I will search >> If this is an algorithm, tell me generally how you would do and let me scratch my head first ? >> >> Thanks Chris >> >> >> [[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.
Boris Steipe
2017-Jun-25 15:23 UTC
[R] Fill in empty cell in data.frame from previous value
Run it through a loop. I assume the cell contents is NA (Not Available). Test for it with is.na(). Whenever that returns TRUE, replace the NA value with the value from the previous row. Cheers, B.> On Jun 24, 2017, at 1:49 PM, Christophe Elek <christophe.elek at gmail.com> wrote: > > Hello Total newbie here... I hope I read the guide properly > > I have the following data.frame (I read it from a CSV file I cannot change) > > names val > 1 Mandy 1 > 2 2 > 3 John 2 > 4 2 > > I want to read the row number 2, but I want the first column to be ?Mandy? and not null > > print (frame[2,]) > 2 Mandy 2 > > I can manipulate the data.frame once loaded > How can I fill all cell in column ?names? with the previous value ? > Or is there a function that will get me the row and fill the ?names? column ? > > NOTA BENE: I do not want the answer, I want to find it myself but I need guidance > If there is a function, tell me the library and I will search > If this is an algorithm, tell me generally how you would do and let me scratch my head first ? > > Thanks Chris > > > [[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.
Christophe Elek
2017-Jun-25 15:25 UTC
[R] Fill in empty cell in data.frame from previous value
Perfect Jim, that Is exactly what I needed ? let me check that ... Cheers From: Jim Lemon Sent: June 25, 2017 3:11 AM To: Christophe Elek Cc: r-help at r-project.org Subject: Re: [R] Fill in empty cell in data.frame from previous value Hi Chris, You may know about the *apply family of functions. These slice various data structures and "apply" a specified function to each slice, usually returning a list of return values. As far as I am aware, you can't access adjacent rows unless you reformat the data structure. There is a way to do this particular job. It requires the sequence operator (:), the ifelse function and indexing. What you do is to create a sequence of all the values in the element "names", then subtract 1 for all the NA (or any other specified value) values and use the resulting vector to index the original element "names". It won't work if the first value is NA, nor will it work for more than one NA in a row. I realize that this is pretty obscure, but you said that you didn't just want the solution. It's a one-liner. Jim On Sun, Jun 25, 2017 at 3:49 AM, Christophe Elek <christophe.elek at gmail.com> wrote:> Hello Total newbie here... I hope I read the guide properly > > I have the following data.frame (I read it from a CSV file I cannot change) > > names val > 1 Mandy 1 > 2 2 > 3 John 2 > 4 2 > > I want to read the row number 2, but I want the first column to be ?Mandy? and not null > > print (frame[2,]) > 2 Mandy 2 > > I can manipulate the data.frame once loaded > How can I fill all cell in column ?names? with the previous value ? > Or is there a function that will get me the row and fill the ?names? column ? > > NOTA BENE: I do not want the answer, I want to find it myself but I need guidance > If there is a function, tell me the library and I will search > If this is an algorithm, tell me generally how you would do and let me scratch my head first ? > > Thanks Chris > > > [[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]]
Christophe Elek
2017-Jun-25 15:26 UTC
[R] Fill in empty cell in data.frame from previous value
Excellent Boris, thx ? this helps From: Boris Steipe Sent: June 25, 2017 11:23 AM To: Christophe Elek Cc: r-help at r-project.org Subject: Re: [R] Fill in empty cell in data.frame from previous value Run it through a loop. I assume the cell contents is NA (Not Available). Test for it with is.na(). Whenever that returns TRUE, replace the NA value with the value from the previous row. Cheers, B.> On Jun 24, 2017, at 1:49 PM, Christophe Elek <christophe.elek at gmail.com> wrote: > > Hello Total newbie here... I hope I read the guide properly > > I have the following data.frame (I read it from a CSV file I cannot change) > > names val > 1 Mandy 1 > 2 2 > 3 John 2 > 4 2 > > I want to read the row number 2, but I want the first column to be ?Mandy? and not null > > print (frame[2,]) > 2 Mandy 2 > > I can manipulate the data.frame once loaded > How can I fill all cell in column ?names? with the previous value ? > Or is there a function that will get me the row and fill the ?names? column ? > > NOTA BENE: I do not want the answer, I want to find it myself but I need guidance > If there is a function, tell me the library and I will search > If this is an algorithm, tell me generally how you would do and let me scratch my head first ? > > Thanks Chris > > > [[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]]
Göran Broström
2017-Jun-25 16:44 UTC
[R] Fill in empty cell in data.frame from previous value
> library(tidyr)> ?fill G?ran On 2017-06-24 19:49, Christophe Elek wrote:> Hello Total newbie here... I hope I read the guide properly > > I have the following data.frame (I read it from a CSV file I cannot change) > > names val > 1 Mandy 1 > 2 2 > 3 John 2 > 4 2 > > I want to read the row number 2, but I want the first column to be ?Mandy? and not null > > print (frame[2,]) > 2 Mandy 2 > > I can manipulate the data.frame once loaded > How can I fill all cell in column ?names? with the previous value ? > Or is there a function that will get me the row and fill the ?names? column ? > > NOTA BENE: I do not want the answer, I want to find it myself but I need guidance > If there is a function, tell me the library and I will search > If this is an algorithm, tell me generally how you would do and let me scratch my head first ? > > Thanks Chris > > > [[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. >