简体   繁体   中英

Using diff function in R

My R codes are as follows:

data <- read.csv(file.choose(), header=TRUE)

# Plot yrs vs total cases as time series
plot(data, xlab="Years", ylab="Total cases", type="o", col="blue", font.lab=2)

# Difference data to make data stationary on mean (remove trend)
plot(diff(as.numeric(data[1,])), ylab="First Difference", col="red", font.lab=2, type="o")

Running the 5th line of code gives only 1 point on the graph.

Why is that so?

Here is how my data look like:

链接 .

First, load your data with custom sep and dec (or read.csv2 for ; separator and , for decimal points) from your data:

data <- read.csv(file.choose(), header=TRUE, sep=";", dec=",")
# OR
data <- read.csv2(file.choose(), header=TRUE)

You can use names of the columns, instead of indexing. Then the second plot can be shown as:

plot(diff(as.numeric(data$Total.cases)), ylab="First Difference", col="red", font.lab=2, type="o")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM