I have a dataset where missing data is represented as -999
. The data goes from 0 to 1.
I would like to plot the data as a line plot, but I don't want the valid numbers to connect to the missing data. For example if I have
x = c(0.29, 0.3, -999, 0.31, 0.4)
I want the line to go from 0.29 to 0.3, break at the missing data, and start again between 0.31, 0.4.
Assuming the above data is in file x.txt, I tried to do the following:
x1 = read.table('x.txt', na.strings=c("-999."))
plot(x1, type='l')
but it doesn't do what I w开发者_开发知识库ant.
Replace the -999
with NA
:
is.na(x) <- (x == -999)
plot(x, type="l")
Is that what you want?
Another way of changing -999
to NA
is:
x[x==-999] = NA
plot(x, type="l")
This gives the same results as @Joshua's nice answer, but may be clearer if you are new to R.
You don't say what it does and how that does not match with what you want.
One problem you could be having is that read.table returns a data frame, so in your code it is trying to plot the data frame, not the x column within the data frame. What happens depends on how many columns your data frame ends up with (something that we cannot guess from your example).
If there are exactly 2 columns then a regular scatterplot is created (but options may not pass through correctly). If x1 has only one column then a strip plot is created which is quite different from the scatterplot created when you give a single vector to the plot.default command.
So maybe
plot( x1[[1]], type='l' )
or something like that is what you want.
If not, show us what your data frame looks like after reading it in:
summary(x1)
str(x1)
精彩评论