How to build a multi-column CSV file?

I am very new to R, so excuse me for the question, maybe stupid.

I have a multi-column CSV (simple comma, without quotes), where the first row is the header, the first column is a continuous integer index, and the remaining 17 columns are the floating point values โ€‹โ€‹of the functions.

The task is to display all 17 rows in one diagram (with the same axes).

It sounds very simple, but actually it is not very obvious.

+8
r csv charts
source share
2 answers

You can use read.csv to enter data as data.frame. Then you have many options to build. I prefer lattice for most investigations.

Two in the bars. Here I create random data for the chart.

 library(lattice) d <- data.frame(index=1:20, x=rnorm(20), y=rnorm(20)) > head(d, n=3) index xy 1 1 -1.065591 0.2422635 2 2 -1.563782 -1.4250984 3 3 1.156537 0.3659411 xyplot(x+y~index, data=d, type='l', auto.key=list(space='right')) 

You can generate a formula from the column names. Usually I do not do this from the hint, but I use such constructs in the code:

 f <- paste(paste(names(d[,-1,drop=FALSE]), collapse="+"), names(d[,1,drop=FALSE]), sep=" ~ ") xyplot(as.formula(f), data=d, type='l', auto.key=list(space='right')) 

As in Ben's answer, type='l' indicates strings. The default value is type='p' for points. I added the auto.key parameter here to mark the series.

enter image description here

+2
source share

Perhaps the most compact base-R solution is

 mydata <- read.csv("mydatafile.csv") matplot(mydata[, 1], mydata[, -1], type="l") 
  • header=TRUE is the default option read.csv() , so you do not need to explicitly indicate the presence of a header line
  • mydata[, 1] selects the first column; mydata[, -1] selects everything except the first column
  • type="l" selects strings (by default, dots); see ?matplot ?plot for details on changing line types, colors, etc. etc. etc.

Once you know that matplot is useful, you can search for StackOverflow for other examples, for example. How to draw multiple lines from csv to R

+18
source share

All Articles