David
David

Reputation: 4895

insert row in data frame without creating new columns with R

I have this :

    1  2  3
1  10 11 12
2  13 14 15
3  16 17 18
4  19 20 21
5  22 23 24

And I want this :

    1     2  3
1  10    11 12 
2  13    14 15
3   0  0.00  
4  16    17 18
5  19    20 21
6  22    23 24

Can anyone tell me how to do that ? I tried rbind, rbind.fill, smartbind, but the result is not what I expect for.

Thank you

Upvotes: 1

Views: 7866

Answers (3)

user3670684
user3670684

Reputation: 1153

for example you want to add rows of variable 2 to variable 1 of a data named "edges" just do it like this

allEdges <- data.frame(c(edges$V1,edges$V2))

Upvotes: 0

asb
asb

Reputation: 4432

How about:

insert.under.row.number <- function (mat, rnum, what) {
  if (length(what) != ncol(mat)) what <- c(what, rep_len(NA, ncol(mat) - length(what)))
  rbind(mat[1:rnum, ], what, mat[(rnum + 1):nrow(mat), ])
}

This will put an NA in the third column if you give what as c(0, 0.00).


Actually, I am just going to link to the possible duplicate that @dickoa linked to: Add new row to dataframe, at specific row-index, not appended?. The only thing you need to do different from that answer is this part:

if (length(what) != ncol(mat)) what <- c(what, rep_len(NA, ncol(mat) - length(what)))

So, on this difference, I am leaving my answer up. But one should check out that answer as well.


Here is a sample from the input file you linked to:

  V1      V2       V3      V4     V5          V6   V7     V8 V9   V10
1  1 4068961 731027.9 186.849 1.5000  sondage006 24.0 26.000 NA 2.000
2  1 4068963 731027.8 185.317 0.9500  sondage006 26.0 28.000 NA 2.000
3  1 4068964 731027.8 183.785 2.1000  sondage006 28.0 30.000 NA 2.000
4  1 4068965 731027.8 182.253 1.3000  sondage006 30.0 32.000 NA 2.000
5  1 4068967 731027.8 180.721 0.5000  sondage006 32.0 34.000 NA 2.000
6  1 4068916 731018.2 181.267 0.5732 sondage006A 25.8 27.667 NA 1.867

and this after doing: yy <- insert.under.row.number(xx, 3, c(0, 0.0))

    V1      V2       V3      V4   V5         V6 V7 V8 V9 V10
1    1 4068961 731027.9 186.849 1.50 sondage006 24 26 NA   2
2    1 4068963 731027.8 185.317 0.95 sondage006 26 28 NA   2
3    1 4068964 731027.8 183.785 2.10 sondage006 28 30 NA   2
4    0       0       NA      NA   NA       <NA> NA NA NA  NA
410  1 4068965 731027.8 182.253 1.30 sondage006 30 32 NA   2
5    1 4068967 731027.8 180.721 0.50 sondage006 32 34 NA   2

The row names seem to have gotten messed up.

Upvotes: 1

Maxim.K
Maxim.K

Reputation: 4180

Your new row is shorter than the rest. Accordingly, you would have to fill in an NA value in the third column:

rbind(mydf[1:2,],c(0,0,NA),mydf[3:nrow(mydf),])

If you insert c(0,0), thus omitting the NA value, R will apply the vector recycling rule. This means, that the shorter vector will be "stretched" to match the required length, recycling its elements, starting again from the first. Thus effectively a vector c(0,0,0) will be inserted. It is up to you to choose the desired behavior (NA vs 0 as required value).

ADDENDUM:

If the number of colums is variable, then you can either use the answer by @asb or use the following approach:

mydf <- rbind(mydf[1:2,],NA,mydf[3:nrow(mydf),])
mydf[3,1:2] <- c(0,0)

Upvotes: 2

Related Questions