[R] Inserting a blank row to every other row

David L Carlson dcarlson at tamu.edu
Mon Apr 25 16:42:07 CEST 2016


This is basically Bert's solution, but simplifying one line.

> z <- data.frame(a=1:3,b = letters[1:3])
> i <- seq_len(nrow(z))
> z <- dat[rep(i, each=2), ]
> is.na(z[i*2, ]) <- TRUE
> z
     a    b
1    1    a
1.1 NA <NA>
2    2    b
2.1 NA <NA>
3    3    c
3.1 NA <NA>

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Bert Gunter
Sent: Sunday, April 24, 2016 11:13 AM
To: Ulrik Stervbo
Cc: R-help Mailing List
Subject: Re: [R] Inserting a blank row to every other row

Oh, sorry, I just realized that I messed up the indicing. Here is the
correct way:

> z <- data.frame(a=1:3,b = letters[1:3])
>  i <- seq_len(nrow(z))
>  z<-z[rep(i,e=2),]
>  z[2*i, ] <- matrix(NA, nr=nrow(z),nc=ncol(z))
>  z

Still doubt that this is a good idea, though.

-- 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, Apr 24, 2016 at 8:53 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
> Well, something like this would work (there may be slicker solutions):
>
>> z <- data.frame(a=1:3,b = letters[1:3])
>> i <- seq_len(nrow(z)) *2
>> z <-rbind(z,z)
>> z[i, ] <- matrix(NA, nr=nrow(z),nc=ncol(z))
>> z
>    a    b
> 1  1    a
> 2 NA <NA>
> 3  3    c
> 4 NA <NA>
> 5  2    b
> 6 NA <NA>
>
> But I agree with you that there is probably a way to handle the
> underlying issues that does not require this kind of artifice.
>
> 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, Apr 24, 2016 at 8:21 AM, Ulrik Stervbo <ulrik.stervbo at gmail.com> wrote:
>> Hi Saba,
>>
>> I don't know how to do what you want and I also cannot see why.
>>
>> If you describe what you hope to achieve there might be a different
>> solution.
>>
>> Best wishes
>> Ulrik
>>
>> Saba Sehrish via R-help <r-help at r-project.org> schrieb am So., 24. Apr.
>> 2016 14:04:
>>
>>> Hi
>>>
>>> I need to insert a blank row after every row in R data frame. I have
>>> achieved it through:
>>>
>>>
>>> df[rep(1:nrow(df),1,each=2),]
>>>
>>> But it inserts a row with name of previous row, while i want a complete
>>> blank row without any name/title.
>>>
>>> Please guide me
>>>
>>> Regards
>>> Saba
>>>
>>> ______________________________________________
>>> 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]]
>>
>> ______________________________________________
>> 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.



More information about the R-help mailing list