mercredi 4 novembre 2015

How can I plot different file in a single plot?

I'm working on R 3.1.1 GUI 1.65 Mavericks build (6784) and I would like to say that I have 3 textual files (file1, file2, file3) which exactly have these format:

-6.302229035036262 -20.0 -1.020941553137966 
-4.04318422254186 -3.583695163352756 -3.6530481033374245 
-2.077324334243253 -20.0 -20.0 
-6.302229035036262 -20.0 -1.020941553137966 
-6.4977283774856875 -10.964877298563136 -1.1022927689594357 
-6.291583374531307 -16.450926383021542 -1.176625625964833 
-6.223879288477839 -12.115100317368787 -1.5212364609954971 

The separator is a blank space, there are not neither column names nor row names, and what is crucial is that the last character is an other blank space separator (" "). Unfortunately the last separator of the row is unremovable since those files are previously generated by an external software.

Here are my steps: 1) I set my working directory where files are located

setwd(“working directory”)

2) I create a matrix from a file (Note that I'm using read.csv even if it has no extension even though it's readable from any kind of editor) I finally save it in a variable

data <- as.matrix(read.csv(“file1″, header=FALSE, sep=” “));

A new [Nx4] matrix is created where R interpret as:

            V1         V2         V3 V4
[1,] -6.302229 -20.000000  -1.020942 NA
[2,] -4.043184  -3.583695  -3.653048 NA
[3,] -2.077324 -20.000000 -20.000000 NA
[4,] -6.302229 -20.000000  -1.020942 NA
[5,] -6.497728 -10.964877  -1.102293 NA
[6,] -6.291583 -16.450926  -1.176626 NA
[7,] -6.223879 -12.115100  -1.521236 NA

The problem now is that I would like to have an Nx3 matrix instead of Nx4, and that problem is caused by the last blank separator. How can I tell R to interpret that the last character/separator should be removed? I would like to get something like this as a result:

            V1         V2         V3
[1,] -6.302229 -20.000000  -1.020942
[2,] -4.043184  -3.583695  -3.653048
[3,] -2.077324 -20.000000 -20.000000
[4,] -6.302229 -20.000000  -1.020942
[5,] -6.497728 -10.964877  -1.102293
[6,] -6.291583 -16.450926  -1.176626
[7,] -6.223879 -12.115100  -1.521236

At that time, I don't know how to plot it in order to change column name with for example: f1, f2 and f3.

Having done the plot, I would like to plot the other 2 files in the same plot, thus the final plot could contain 3 different output.

Thank you all for collaboration.

Aucun commentaire:

Enregistrer un commentaire