How can I add rows to an R data frame every other row?

前端 未结 5 1469
情书的邮戳
情书的邮戳 2020-12-19 07:49

Brief: how can I add m rows to my m X n data frame, where each new row is inserted after each existing row? I will essentially copy the existing ro

相关标签:
5条回答
  • 2020-12-19 08:22

    You could create a new matrix with twice as many rows, put the old data frame elements back into the appropriate positions of the new matrix, and leave the gaps. Perform the calculations on elevation, creating a new matrix, then insert the adjusted elevation matrix into the larger, new matrix. Then convert the matrix back to a data frame.

    test <- matrix(1:9, ncol=3)
    ind <- (1:nrow(test)*2 - 1 # - 1 b/c you want to insert rows after, not before, existing rows
    test_new <- matrix(rep(NA, (nrow(test)*2*ncol(test))), ncol=ncol(test))
    test_new[ind,] <- test
    
    test_elev <- test #create a new matrix that will have adjusted elevations
    test_elev[,2] <- test[,2] - test[,3] #e.g., test[,2] is the elevation column, and test[,3] is the length column
    test_new[ind+1,] <- test_elev #then put the new elevations into the new matrix
    
    #if you need it to be a data.frame() again, you can use `as.data.frame(test_new)`
    
    0 讨论(0)
  • 2020-12-19 08:25

    I am not sure how rgl comes into play here, but if you just want to create a new data.frame with repeated values, try:

    df[rep(1:nrow(df),1,each=2),]
    
    0 讨论(0)
  • 2020-12-19 08:29

    Here's one possible approach if I understand what you're doing:

    dat <- head(CO2, 10) # fake data set
    
    L1 <- lapply(1:nrow(dat), function(i) {
        dat2x <-  dat[i, ]
        dat2x[4] <- dat[i, 4] - dat[i, 5]
        rbind(dat[i, ], dat2x)
    })
    do.call(rbind, L1)
    

    EDIT: totally working off e4e5f4's excellent response:

    new <- dat[rep(1:nrow(dat),1,each=2),]
    new[c(F, T),4] <- dat[4] - dat[5]
    

    Both are equivalent but I assume the alter is way faster.

    0 讨论(0)
  • 2020-12-19 08:30

    Your sample data:

    orig.df <- read.table(text = "
    Label easting northing elevation length
    47063  554952  5804714 32.68 619.25 
    47311  492126  5730703 10.40 1773.00", header = TRUE)
    

    Create your data to be inserted:

    insert.df <- transform(orig.df, elevation = elevation - length)
    

    Append it to your original data:

    out.df <- rbind(orig.df, insert.df)
    

    Reorder the rows:

    n <- nrow(orig.df)
    out.df[kronecker(1:n, c(0, n), "+"), ]
    #   Label easting northing elevation  length
    # 1 47063  554952  5804714     32.68  619.25
    # 3 47063  554952  5804714   -586.57  619.25
    # 2 47311  492126  5730703     10.40 1773.00
    # 4 47311  492126  5730703  -1762.60 1773.00
    
    0 讨论(0)
  • 2020-12-19 08:33

    modified from "e4e5f4's" response

    Insert blank rows betweens rows

        # sample matrix of df 
        old <-matrix(1:9, ncol=3)
    
        # double all rows 
        new <- old[rep(1:nrow(old),1,each=2),]
    
        # replace all duplicates with blank cells
        new[c(seq(2, dim(new)[1], by=2)), ] <- ""
    
        old # original 
        new # all ok ;)
    
    0 讨论(0)
提交回复
热议问题