y <- c(1, 2, 3, NA)
is.na(y)
is.na用以识别是否有缺失值,返回逻辑值
# Recode 99 to missing for the variable age
leadership[age == 99, "age"] <- NA#定义列名称为age的列,此时行值为99的时候为NA
leadership
确实缺失值
# 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
运算前必须删除缺失值
leadership
newdata <- na.omit(leadership)
newdata
na.omit删除缺失行
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)
利用format参数设定个格式
myformat <- "%m/%d/%y"
str(leadership)
leadership$date <- as.Date(leadership$date, myformat)
str(leadership)
转换成日期格式
today <- Sys.Date()
format(today, format="%B %d %Y")
format(today, format="%A")
format指定和提取日期信息
startdate <- as.Date("2004-02-13")
enddate <- as.Date("2009-06-22")
enddate - startdate
日期的计算
today <- Sys.Date()
dob <- as.Date("1966-10-12")
difftime(today, dob, units="days")
利用diftime函数计算日期间隔并指定时间单位
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
网友评论