How to plot a multicolumn CSV file?

前端 未结 2 1465
攒了一身酷
攒了一身酷 2021-02-04 09:23

I am very new to R, so excuse me for a question probably stupid.

I\'ve got a multicolumn CSV (plain comma-separated, no quotes) file where the first row is the header, t

2条回答
  •  醉梦人生
    2021-02-04 09:56

    Probably the most compact, base-R-only solution is

    mydata <- read.csv("mydatafile.csv")
    matplot(mydata[, 1], mydata[, -1], type="l")
    
    • header=TRUE is a default option to read.csv(), so you don't need to specify the existence of the header row explicitly
    • mydata[, 1] selects the first column; mydata[, -1] selects all but the first column
    • type="l" selects lines (the default is points); see ?matplot, ?plot for details of changing line types, colours, etc etc etc ...

    Once you know that matplot is useful you can search StackOverflow for other examples, e.g. How to draw multiple Lines from csv in R

提交回复
热议问题