Removing Empty Rows of a Data File in R

Removing empty rows of a data file in R

I assume you want to remove rows that are all NAs. Then, you can do the following :

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

If you want to remove rows that have at least one NA, just change the condition :

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

Removing all empty columns and rows in data.frame when rows don't go away

You have NA and also empty rows. You can do

B1[rowSums(is.na(B1) | B1 == "") != ncol(B1), ]

# study.name group.name outcome ESL prof scope type
#1 Shin.Ellis ME.short 1 1 2 1 1
#2 Shin.Ellis ME.long 1 1 2 1 1
#3 Shin.Ellis DCF.short 1 1 2 1 2
#4 Shin.Ellis DCF.long 1 1 2 1 2
#5 Shin.Ellis Cont.short 1 1 2 NA NA
#6 Shin.Ellis Cont.long 1 1 2 NA NA
#8 Trus.Hsu Exper 1 2 2 2 1
#.....

We can also use filter_all from dplyr

library(dplyr)
B1 %>% filter_all(any_vars(!is.na(.) & . != ""))

Delete rows with blank values in one particular column

 df[!(is.na(df$start_pc) | df$start_pc==""), ]

R) how to remove rows with empty values?

Try something like this

test[rowSums(is.na(test))!=ncol(test), ] # first set blank to NA

or

test[rowSums(test=="")!=ncol(test), ]

Remove data below a completely blank row

Given AdamO's suggestion and a very similar question that was found with a good answer. This is adapted from the answer to that question given by @sinQueso.

Split the output which has been read in as a dataframe rawdata

# add column to indicate groups
rawdata$tbl_id <- cumsum(!nzchar(rawdata$Machine.Database.Output))

## remove blank lines
rawdata <- rawdata[nzchar(rawdata$Machine.Test.Database.Output), ]

## split the data frame
dt_s <- split(rawdata[, -ncol(rawdata)], rawdata$tbl_id)

## use first line as header and reset row numbers
dt_s <- lapply(dt_s, function(x) {
colnames(x) <- x[1, ]
x <- x[-1, ]
rownames(x) <- NULL
x
})

##to send all list parts out to their own dataframe
list2env(x=dt_s, envir = .GlobalEnv)

Skip/delete rows having empty cells

@maydin's link works great for NA values, but you'll need a little bit more to check for a specific value (i.e, "", the empty string).

df <- data.frame(a=c('a','b',''), b="")
rowSums(df != "") == 0
# [1] FALSE FALSE TRUE

That tells you which rows have exactly 0 non-empty strings on the row. If even one of the columns has something more than zero-length-string, then it'll pop as false.

Using this, we'll look for only rows with 1 or more non-empty-strings.

df[rowSums(df != "") > 0, ]
# a b
# 1 a
# 2 b

How to delete empty rows in multiple dataframes in a list

Consider a combination of those attempted solutions. Because complete.cases() checks for NA in all columns and empty strings values are NOT the same as NA, we reassign values and then run completed.cases. Below data.frame() is called to avoid vector return due to the single column (remove call for more than one column).

df_list <- list(
data.frame(sn=c("","","","A","","B","","","C")),
data.frame(sn=c("","","","A","","B","","","C")),
data.frame(sn=c("","","","A","","B","","","C"))
)

new_df_list <- lapply(df_list, function(df) {
df[df == ""] <- NA
data.frame(sn = df[complete.cases(df),])
})

new_df_list
# [[1]]
# sn
# 1 A
# 2 B
# 3 C

# [[2]]
# sn
# 1 A
# 2 B
# 3 C

# [[3]]
# sn
# 1 A
# 2 B
# 3 C

how to remove empty line with fread() in r

fread also has a skip parameter that you can use, but it is slightly different than read.csv.raw. So you can probably read the header first, then read the content using skip

How to skip empty rows while reading multiple tabs in R?

perhaps the easiest solution would be to read it as it is then remove rows, i.e. yourdata <- yourdata[!is.na(yourdata$columname),] ; this would work if you don't expect any NA's in a particular column, like id. If you have data gaps everywhere you can test for all NAs in multiple columns - let me know if that's what you need.



Related Topics



Leave a reply



Submit