Programing

R에서 데이터 파일의 빈 행 제거

lottogame 2020. 11. 11. 07:50
반응형

R에서 데이터 파일의 빈 행 제거


빈 행이있는 데이터 세트가 있습니다. 제거하고 싶습니다.

myData<-myData[-which(apply(myData,1,function(x)all(is.na(x)))),]

잘 작동합니다. 하지만 이제 데이터에 열을 추가하고 첫 번째 값을 초기화하고 싶습니다.

myData$newCol[1] <- -999

Error in `$<-.data.frame`(`*tmp*`, "newCol", value = -999) : 
  replacement has 1 rows, data has 0

불행히도 작동하지 않으며 이유를 이해하지 못하고 이것을 해결할 수 없습니다. 다음을 사용하여 한 번에 한 줄을 제거하면 작동했습니다.

TgData = TgData[2:nrow(TgData),]

또는 비슷한 것.

처음 13.000 행만 사용했을 때도 작동합니다.

그러나 32.000 행의 실제 데이터에서는 작동하지 않습니다.

내가 뭘 잘못 했어? 제게 말이 안되는 것 같습니다.


모든 NA 인 행을 제거하고 싶다고 가정합니다. 그런 다음 다음을 수행 할 수 있습니다.

data <- rbind(c(1,2,3), c(1, NA, 4), c(4,6,7), c(NA, NA, NA), c(4, 8, NA)) # sample data
data
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1   NA    4
[3,]    4    6    7
[4,]   NA   NA   NA
[5,]    4    8   NA

data[rowSums(is.na(data)) != ncol(data),]
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1   NA    4
[3,]    4    6    7
[4,]    4    8   NA

NA가 하나 이상있는 행을 제거하려면 조건을 변경하십시오.

data[rowSums(is.na(data)) == 0,]
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    6    7

NA가 아닌 빈 행이있는 경우 다음을 수행 할 수 있습니다.

data[!apply(data == "", 1, all),]

둘 다 제거하려면 (NA 및 비어 있음) :

data <- data[!apply(is.na(data) | data == "", 1, all),]

패키지를 NA사용 하는 행의 대체 솔루션janitor

myData %>% remove_empty("rows")

Here are some dplyr options:

# sample data
df <- data.frame(a = c('1', NA, '3', NA), b = c('a', 'b', 'c', NA), c = c('e', 'f', 'g', NA))

library(dplyr)

# remove rows where all values are NA:
df %>% filter_all(any_vars(!is.na(.)))
df %>% filter_all(any_vars(complete.cases(.)))  


# remove rows where only some values are NA:
df %>% filter_all(all_vars(!is.na(.)))
df %>% filter_all(all_vars(complete.cases(.)))  

# or more succinctly:
df %>% filter(complete.cases(.))  
df %>% na.omit

# dplyr and tidyr:
library(tidyr)
df %>% drop_na

This is similar to some of the above answers, but with this, you can specify if you want to remove rows with a percentage of missing values greater-than or equal-to a given percent (with the argument pct)

drop_rows_all_na <- function(x, pct=1) x[!rowSums(is.na(x)) >= ncol(x)*pct,]

Where x is a dataframe and pct is the threshold of NA-filled data you want to get rid of.

pct = 1 means remove rows that have 100% of its values NA. pct = .5 means remome rows that have at least half its values NA

참고URL : https://stackoverflow.com/questions/6437164/removing-empty-rows-of-a-data-file-in-r

반응형