ailing 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
Starting from this data frame:
my.df <- data.frame(num = 1:5, let = letters[1:5])
> my.df
num let
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
>
and inserting a blank row (NAs row) for each one of my.df rows.
na.df <- data.frame(num = NA, let = NA)
my.df <- do.call(rbind, apply(my.df,
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
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
ab
1 1a
2 NA
3 3c
4 NA
5 2b
6 NA
But I agree with you that there
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 schrieb am So., 24. Apr.
2016 14:04:
> Hi
>
> I need to insert a blank row
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
6 matches
Mail list logo