How to read rows above and below a specified value

Viewed 81

I have some data on a table to be read (currently using read.table), but the minimum value that I have on the table is 27, and the maximum value is 1000. I need to set the read.table to only read values >180 and <800. My data is too large to go by hand (700k+ rows). Is there any way to do it?

Right now, my code looks like this:

data1 <- read.table('cn_EP27_L1.sizes')
  names(data1) <- 'sizes1' 
  data1

Table content:
Table content

2 Answers

Using awk with read.table

df1 <- read.table(pipe("awk 'BEGIN {FS=\" \"} {if ($1 >180 && $1 < 800) print $0}' cn_EP27_L1.sizes.txt"))

Try package sqldf, function read.csv.sql. It allows to filter the rows (or columns) with SQL statements.

library(sqldf)

sql <- "select * from file where V1 > 180 and V1 < 800"
df2 <- read.csv.sql(file = "test_read.txt", sql, sep = " ")

After testing,

unlink("test_read.txt")

Test data file

n <- 700e3
set.seed(2021)
df1 <- matrix(sample(27:1000, 2*n, TRUE), ncol = 2)
df1 <- as.data.frame(df1)
write.table(df1, "test_read.txt", sep = " ", 
            quote = FALSE, row.names = FALSE)
Related