y <- c(1, 2, 3, NA)
is.na(y)
![](https://img.haomeiwen.com/i22546862/24899348e4c3d60c.png)
# Recode 99 to missing for the variable age
leadership[age == 99, "age"] <- NA#定义列名称为age的列,此时行值为99的时候为NA
leadership
![](https://img.haomeiwen.com/i22546862/854fd896ba748b8d.png)
# Excluding missing values from analyses
x <- c(1, 2, NA, 3)
y <- x[1] + x[2] + x[3] + x[4]
z <- sum(x)
e <- sum(x, na.rm=TRUE)#删除缺失值后运算才能有结果
y;z;e
![](https://img.haomeiwen.com/i22546862/616ab1e94ee305f2.png)
leadership
newdata <- na.omit(leadership)
newdata
![](https://img.haomeiwen.com/i22546862/56a94ff71441ec51.png)
mydates <- as.Date(c("2007-06-22", "2004-02-13"))
str(mydates)
![利用as.Date]函数进行日期格式的转化(https://img.haomeiwen.com/i22546862/ba79a4a4eb5f64f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
strDates <- c("01/05/1965", "08/16/1975")
class(strDates)
dates <- as.Date(strDates, format = "%m/%d/%Y")
class(dates)
![](https://img.haomeiwen.com/i22546862/87aeeb4966568262.png)
myformat <- "%m/%d/%y"
str(leadership)
leadership$date <- as.Date(leadership$date, myformat)
str(leadership)
![](https://img.haomeiwen.com/i22546862/ab0ae35ac8c58937.png)
today <- Sys.Date()
format(today, format="%B %d %Y")
format(today, format="%A")
![](https://img.haomeiwen.com/i22546862/1cc89242d18c5210.png)
startdate <- as.Date("2004-02-13")
enddate <- as.Date("2009-06-22")
enddate - startdate
![](https://img.haomeiwen.com/i22546862/f91126cbeb1bda98.png)
today <- Sys.Date()
dob <- as.Date("1966-10-12")
difftime(today, dob, units="days")
![](https://img.haomeiwen.com/i22546862/f3688c5e574d96f2.png)
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
网友评论