skip some rows in read.csv in R

后端 未结 3 1576
無奈伤痛
無奈伤痛 2020-12-03 11:14

I have a csv file which I read using the following function:

csvData <- read.csv(file=\"pf.csv\", colClasses=c(NA, NA,\"NULL\",NA,\"NULL\",NA,\"NULL\",\"         


        
3条回答
  •  忘掉有多难
    2020-12-03 12:01

    It is possible using sqldf package, using read.csv.sql

    Lets say the contents of sample.csv looks like this:

    id,name,age
    1,"a",23
    2,"b",24
    3,"c",23
    

    Now to read only rows where age=23:

    require(sqldf)
    
    df <- read.csv.sql("sample.csv", "select * from file where age=23")
    
    df
      id name age
    1  1  "a"  23
    2  3  "c"  23
    

    It is possible to select necessary columns:

    df <- read.csv.sql("sample.csv", "select id, name from file where age=23")
    df
      id name
    1  1  "a"
    2  3  "c"
    

提交回复
热议问题