Program Club

R에서 데이터 프레임의 첫 번째 행을 삭제하는 방법은 무엇입니까?

proclub 2020. 10. 13. 19:34
반응형

R에서 데이터 프레임의 첫 번째 행을 삭제하는 방법은 무엇입니까?


각각 1000 개가 넘는 행이있는 11 개의 열이있는 데이터 세트가 있습니다. 열은 V1, V2, V11 등으로 레이블이 지정되었습니다. "c"명령을 사용하여 이름을 더 유용한 것으로 대체했습니다. 행 1에도 각 열에 대한 레이블이 포함되어 있고 실제 데이터가 행 2에서 시작된다는 사실을 몰랐습니다.

행 1을 삭제하고 감소시키는 방법이 있습니까?


다음과 같이 원본 파일의 레이블을 유지하십시오.

df = read.table('data.txt', header = T)

x 및 y라는 열이있는 경우 다음과 같이 주소를 지정할 수 있습니다.

df$x
df$y

data.frame에서 첫 번째 행을 실제로 삭제하려면 다음과 같은 음수 인덱스를 사용할 수 있습니다.

df = df[-1,]

data.frame에서 열을 삭제하려면 NULL을 할당 할 수 있습니다.

df$x = NULL

다음은 R에서 data.frame을 만들고 조작하는 방법에 대한 몇 가지 간단한 예입니다.

# create a data.frame with 10 rows
> x = rnorm(10)
> y = runif(10)
> df = data.frame( x, y )

# write it to a file
> write.table( df, 'test.txt', row.names = F, quote = F )

# read a data.frame from a file: 
> read.table( df, 'test.txt', header = T )

> df$x
 [1] -0.95343778 -0.63098637 -1.30646529  1.38906143  0.51703237 -0.02246754
 [7]  0.20583548  0.21530721  0.69087460  2.30610998
> df$y
 [1] 0.66658148 0.15355851 0.60098886 0.14284576 0.20408723 0.58271061
 [7] 0.05170994 0.83627336 0.76713317 0.95052671

> df$x = x
> df
            y           x
1  0.66658148 -0.95343778
2  0.15355851 -0.63098637
3  0.60098886 -1.30646529
4  0.14284576  1.38906143
5  0.20408723  0.51703237
6  0.58271061 -0.02246754
7  0.05170994  0.20583548
8  0.83627336  0.21530721
9  0.76713317  0.69087460
10 0.95052671  2.30610998

> df[-1,]
            y           x
2  0.15355851 -0.63098637
3  0.60098886 -1.30646529
4  0.14284576  1.38906143
5  0.20408723  0.51703237
6  0.58271061 -0.02246754
7  0.05170994  0.20583548
8  0.83627336  0.21530721
9  0.76713317  0.69087460
10 0.95052671  2.30610998

> df$x = NULL
> df 
            y
1  0.66658148
2  0.15355851
3  0.60098886
4  0.14284576
5  0.20408723
6  0.58271061
7  0.05170994
8  0.83627336
9  0.76713317
10 0.95052671

네거티브 인덱싱을 사용하여 행을 제거 할 수 있습니다. 예 :

dat <- dat[-1, ]

다음은 그 예입니다.

> dat <- data.frame(A = 1:3, B = 1:3)
> dat[-1, ]
  A B
2 2 2
3 3 3
> dat2 <- dat[-1, ]
> dat2
  A B
2 2 2
3 3 3

That said, you may have more problems than just removing the labels that ended up on row 1. It is more then likely that R has interpreted the data as text and thence converted to factors. Check what str(foo), where foo is your data object, says about the data types.

It sounds like you just need header = TRUE in your call to read in the data (assuming you read it in via read.table() or one of it's wrappers.)


No one probably really wants to remove row one. So if you are looking for something meaningful, that is conditional selection

#remove rows that have long length and "0" value for vector E

>> setNew<-set[!(set$length=="long" & set$E==0),]

dat <- dat[-1, ] worked but it killed my dataframe, changing it into another type. Had to instead use dat <- data.frame(dat[-1, ]) but this is possibly a special case as this dataframe initially had only one column.


I am not expert, but this may work as well,

dat <- dat[2:nrow(dat), ]

While I agree with the most voted answer, here is another way to keep all rows except the first:

dat <- tail(dat, -1)

This can also be accomplished using Hadley Wickham's dplyr package.

dat <- dat %>% slice(-1)

참고URL : https://stackoverflow.com/questions/7541610/how-to-delete-the-first-row-of-a-dataframe-in-r

반응형