programing

추가되지 않은 특정 행 인덱스에서 데이터 프레임에 새 행을 추가하시겠습니까?

stoneblock 2023. 6. 28. 21:16

추가되지 않은 특정 행 인덱스에서 데이터 프레임에 새 행을 추가하시겠습니까?

다음 코드는 벡터와 데이터 프레임을 결합한 것입니다.

newrow = c(1:4)
existingDF = rbind(existingDF,newrow)

그러나 이 코드는 항상 데이터 프레임 끝에 새 행을 삽입합니다.

데이터 프레임 내의 지정된 지점에 행을 삽입하려면 어떻게 해야 합니까?예를 들어, 데이터 프레임에 20개의 행이 있다고 가정해 보겠습니다. 10행과 11행 사이에 새 행을 삽입하려면 어떻게 해야 합니까?

다음은 이러한 문제를 방지하는 솔루션입니다(종종 느린 속도가 느립니다.rbind호출:

existingDF <- as.data.frame(matrix(seq(20),nrow=5,ncol=4))
r <- 3
newrow <- seq(4)
insertRow <- function(existingDF, newrow, r) {
  existingDF[seq(r+1,nrow(existingDF)+1),] <- existingDF[seq(r,nrow(existingDF)),]
  existingDF[r,] <- newrow
  existingDF
}

> insertRow(existingDF, newrow, r)
  V1 V2 V3 V4
1  1  6 11 16
2  2  7 12 17
3  1  2  3  4
4  3  8 13 18
5  4  9 14 19
6  5 10 15 20

속도가 명확성보다 덜 중요하다면, @Simon의 솔루션은 잘 작동합니다.

existingDF <- rbind(existingDF[1:r,],newrow,existingDF[-(1:r),])
> existingDF
   V1 V2 V3 V4
1   1  6 11 16
2   2  7 12 17
3   3  8 13 18
4   1  2  3  4
41  4  9 14 19
5   5 10 15 20

(인덱스를 사용합니다.r다르게)

마지막으로 벤치마크:

library(microbenchmark)
microbenchmark(
  rbind(existingDF[1:r,],newrow,existingDF[-(1:r),]),
  insertRow(existingDF,newrow,r)
)

Unit: microseconds
                                                    expr     min       lq   median       uq       max
1                       insertRow(existingDF, newrow, r) 660.131 678.3675 695.5515 725.2775   928.299
2 rbind(existingDF[1:r, ], newrow, existingDF[-(1:r), ]) 801.161 831.7730 854.6320 881.6560 10641.417

벤치마크

@MathewDowle이 항상 지적하듯이 문제의 크기가 증가함에 따라 확장을 위한 벤치마크를 검토해야 합니다.자, 이제 시작합니다.

benchmarkInsertionSolutions <- function(nrow=5,ncol=4) {
  existingDF <- as.data.frame(matrix(seq(nrow*ncol),nrow=nrow,ncol=ncol))
  r <- 3 # Row to insert into
  newrow <- seq(ncol)
  m <- microbenchmark(
   rbind(existingDF[1:r,],newrow,existingDF[-(1:r),]),
   insertRow(existingDF,newrow,r),
   insertRow2(existingDF,newrow,r)
  )
  # Now return the median times
  mediansBy <- by(m$time,m$expr, FUN=median)
  res <- as.numeric(mediansBy)
  names(res) <- names(mediansBy)
  res
}
nrows <- 5*10^(0:5)
benchmarks <- sapply(nrows,benchmarkInsertionSolutions)
colnames(benchmarks) <- as.character(nrows)
ggplot( melt(benchmarks), aes(x=Var2,y=value,colour=Var1) ) + geom_line() + scale_x_log10() + scale_y_log10()

@Rolland의 솔루션은 다음과 같은 요구에도 불구하고 확장성이 매우 뛰어납니다.rbind:

                                                              5       50     500    5000    50000     5e+05
insertRow2(existingDF, newrow, r)                      549861.5 579579.0  789452 2512926 46994560 414790214
insertRow(existingDF, newrow, r)                       895401.0 905318.5 1168201 2603926 39765358 392904851
rbind(existingDF[1:r, ], newrow, existingDF[-(1:r), ]) 787218.0 814979.0 1263886 5591880 63351247 829650894

선형 척도로 표시:

linear

로그 로그 척도:

log-log

insertRow2 <- function(existingDF, newrow, r) {
  existingDF <- rbind(existingDF,newrow)
  existingDF <- existingDF[order(c(1:(nrow(existingDF)-1),r-0.5)),]
  row.names(existingDF) <- 1:nrow(existingDF)
  return(existingDF)  
}

insertRow2(existingDF,newrow,r)

  V1 V2 V3 V4
1  1  6 11 16
2  2  7 12 17
3  1  2  3  4
4  3  8 13 18
5  4  9 14 19
6  5 10 15 20

microbenchmark(
+   rbind(existingDF[1:r,],newrow,existingDF[-(1:r),]),
+   insertRow(existingDF,newrow,r),
+   insertRow2(existingDF,newrow,r)
+ )
Unit: microseconds
                                                    expr     min       lq   median       uq      max
1                       insertRow(existingDF, newrow, r) 513.157 525.6730 531.8715 544.4575 1409.553
2                      insertRow2(existingDF, newrow, r) 430.664 443.9010 450.0570 461.3415  499.988
3 rbind(existingDF[1:r, ], newrow, existingDF[-(1:r), ]) 606.822 625.2485 633.3710 653.1500 1489.216

.before에 있어서의 논쟁.dplyr::add_row행을 지정하는 데 사용할 수 있습니다.

dplyr::add_row(
  cars,
  speed = 0,
  dist = 0,
  .before = 3
)
#>    speed dist
#> 1      4    2
#> 2      4   10
#> 3      0    0
#> 4      7    4
#> 5      7   22
#> 6      8   16
#> ...

당신은 dplyr 패키지를 시도해야 합니다.

library(dplyr)
a <- data.frame(A = c(1, 2, 3, 4),
               B = c(11, 12, 13, 14))


system.time({
for (i in 50:1000) {
    b <- data.frame(A = i, B = i * i)
    a <- bind_rows(a, b)
}

})

산출량

   user  system elapsed 
   0.25    0.00    0.25

rbind 함수를 사용하는 것과 대조적으로

a <- data.frame(A = c(1, 2, 3, 4),
                B = c(11, 12, 13, 14))


system.time({
    for (i in 50:1000) {
        b <- data.frame(A = i, B = i * i)
        a <- rbind(a, b)
    }

})

산출량

   user  system elapsed 
   0.49    0.00    0.49 

약간의 성능 향상이 있습니다.

데이터 프레임에서 다섯 행 뒤에 빈 행을 삽입하고 이 라이브러리 패키지를 사용합니다.

library(berryFunctions)

df <- insertRows(df, 5 , new = "")

언급URL : https://stackoverflow.com/questions/11561856/add-new-row-to-dataframe-at-specific-row-index-not-appended