I am reading a csv
file and unfortunately my dataframe has many missing values. A small snip is as following:
df <- data.frame(Size= c(800, 850, 1100, 1200, 1000),
Value= c(900, NA, 1300, 1100, NA),
Location= c(NA, 'midcity', 'uptown', NA, 'Lakeview'),
Num1 = c(2, NA, 3, 2, NA),
Num2 = c(2,3,3,1,2),
Rent= c('y', 'y', 'n', 'y', 'n'))
I want to predict some of the results using weka
but I can't do it if I have multiple attributes missing. I know that I should be using the function is.na
but I am not sure in what way it can be done because so far I used it only for summing and counting.
Edit: For an example, in this file I have missing values at 4 out of the 5 instances. Instances 2 and 5 share the same missing attributes (B and D), while instances 1 and 4 share the same missing value as well (C). What I'd like to get is a dataframe that consists out of those instances so I could export them into files and run analysis on those files individually. An example of an output could be
> A
> B
Edit 2:
I want to save the splits and so far I tried this:
write.csv(split(temp, index), file = "C:/Users/Nikita/Desktop/splits.csv", row.names=FALSE)
But it writes all the splits in one line. Is there a way to separate them by a line?
Edit 3:
My steps are:
data <- read.csv("location")
index <- apply(is.na(data)*1, 1,paste, collapse = "")
s <- split(data, index)
lapply(s, function(x) {names(x) <- names(data);x})
big.data <- do.call(rbind, s)
write.csv(big.data, file = "location", row.names=FALSE)
Am I missing something?
And
In the future, please create a reproducible example so that users do not have to create a data frame by hand from your question. Pictures are not as helpful.
Data
To combine it all use lapply since
split
creates a list:With a for loop: