Reading two-line headers in R

前端 未结 5 749
梦如初夏
梦如初夏 2020-11-29 05:22

What is the best way to read a file into R when the header has two necessary lines for the header?

This happens to me all the time, as people often use one line for

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 05:42

    Use readLines with 2 for the limit, parse it, paste0 them together, then read in with read.table with skip =2 and header=FALSE (the default). Finish the process off with assignment of the column names:

    dat <- "trt biomass yield
     crop   Mg/ha   bu/ac
     C2 17.76   205.92
     C2 17.96   207.86
     CC 17.72   197.22
     CC 18.42   205.20
     CCW    18.15   200.51
     CCW    17.45   190.59
     P  3.09    0.00
     P  3.34    0.00
     S2 5.13    49.68
     S2 5.36    49.72
     "
    

    You would probably use a file argument but using the text argument to the read-functions makes this more self-contained:

     readLines(textConnection(dat),n=2)
    #[1] "trt\tbiomass\tyield" "crop\tMg/ha\tbu/ac" 
     head2 <- read.table(text=readLines(textConnection(dat),n=2), sep="\t", stringsAsFactors=FALSE)
     with(head2, paste0(head2[1,],head2[2,]) )
    # [1] "trtcrop"      "biomassMg/ha" "yieldbu/ac"  
     joinheadrs <- with(head2, paste0(head2[1,],head2[2,]) )
    
    newdat <- read.table(text=dat, sep="\t",skip=2)
    colnames(newdat)<- joinheadrs
    #-------------------
    > newdat
       trtcrop biomassMg/ha yieldbu/ac
    1       C2        17.76     205.92
    2       C2        17.96     207.86
    3       CC        17.72     197.22
    4       CC        18.42     205.20
    5      CCW        18.15     200.51
    6      CCW        17.45     190.59
    7        P         3.09       0.00
    8        P         3.34       0.00
    9       S2         5.13      49.68
    10      S2         5.36      49.72
    

    Might be better to use paste with an underscore-sep:

    joinheadrs <- with(head2, paste(head2[1,],head2[2,] ,sep="_")  )
    joinheadrs
    #[1] "trt_crop"      "biomass_Mg/ha" "yield_bu/ac"  
    

提交回复
热议问题