Вы находитесь на странице: 1из 2

# Input / Output R cook book

# Objects Advanced R, DS_R, Data_structures

# Subsetting Advanced R

# Almost any operation involving an unknown value will also be unknown.

NA > 5

10 == NA

NA + 10

NA / 2

NA == NA

## Create a vector with NAs in it

x <- c(1, 2, NA, 10, 3)

## Return a logical vector indicating which elements are NA

is.na(x) # x == NA

#[1] FALSE FALSE TRUE FALSE FALSE

## Return a logical vector indicating which elements are NaN 0/0 Inf-Inf

is.nan(x)

#[1] FALSE FALSE FALSE FALSE FALSE

## Now create a vector with both NA and NaN values

x <- c(1, 2, NaN, NA, 4)

is.na(x)

#[1] FALSE FALSE TRUE TRUE FALSE

is.nan(x)

#[1] FALSE FALSE TRUE FALSE FALSE

dfrm <- read.table("filename.txt", na.strings=".")

read.csv(file.choose(), header=TRUE, stringsAsFactors = F ) -> cresult

for(i in 4:21)
{
cresult[,i] <- as.integer(cresult[,i])
}
cresult[!complete.cases(cresult),]

nrow(cresult[!complete.cases(cresult),])

# The following code gives you the number of unknown values in each row of the
dataset

apply(cresult, 1, function(x) sum(is.na(x)))

# Find the rows in a dataset that have a certain number of unknowns.

# cresult[!complete.cases(cresult$X306E), "X306E"] <- mean(cresult$X306E, na.rm =


T)

cresult[!complete.cases(cresult$X306E), "X306E"] <- mean(cresult$X306E, na.rm = T)

cor(cresult[, 4:21], use = "complete.obs")

symnum(cor(cresult[, 4:21], use = "complete.obs"))

lm(X306E ~ X306I, data = cresult)

cresult <- na.omit(cresult) # cresult <- cresult[-c(88,102),]

Вам также может понравиться