Identifying and removing "NA"s and other errors [duplicate] - r

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Test for NA and select values based on result
Suppose you have a vector -- you do a calculation on the vector -- many of the elements return "NA" -- how do you identify these "NA"s and change them to some usable integer

Assuming that your data is in dat (could be a vector, matrix, or data frame):
dat[is.na(dat)]<-0
replaces all NA entries of dat with 0.

Related

Count occurrences of value in a set of variables in R (per column) [duplicate]

This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 1 year ago.
I have this data and I want to figure out a way to know how many ones and how many zeros are in each column (ie Arts and Crafts). I have been trying different things but it hasn't been working. Does anyone have any suggestions?
You can use the table() function in R. This creates a categorical representation of your data. Additionally here convert list to vector I have used unlist() function.
df1 <- read.csv("Your_CSV_file_name_here.csv")
table(unlist(df1$ArtsAndCrafts))
If you want to row vice categorize the number of zeros and ones you can refer to this question in Stackoverflow.

My dataset as "?" values. How can I find all the missing values with R? [duplicate]

This question already has answers here:
Replacing character values with NA in a data frame
(7 answers)
Closed 2 years ago.
I'm new to R and I'm trying to find all the missing values in my data set. I'm using R and I tried the is.na function and it didn't work. Missing entries in my dataset have a "?"
df[df=="?"] <- NA

is there a way to set all cells in a dataframe in the form of a vector as NA? [duplicate]

This question already has answers here:
R: Count number of objects in list [closed]
(5 answers)
Closed 2 years ago.
I have a dataframe in R, and I am trying to set all cells in the form of a vector, either c(1,2,3) or 1:2 to NA. Is there any easy way to do this?
You can use lengths to count number of elements in each value of column. Set them to NA where the length is greater than 1. Here I am considering dataframe name as df and column name as col_name. Change them according to your data.
df$col_name[lengths(df$col_name) > 1] <- NA

Select only numeric variables of a data frame in R [duplicate]

This question already has answers here:
Why does apply convert logicals in data frames to strings of 5 characters?
(2 answers)
Selecting only numeric columns from a data frame
(12 answers)
Closed 2 years ago.
I know that the question is very easy, but I have a more specific one:
I have a data frame, with 50 variables (numeric and non-numeric) and 5000 observations.
Now what I want to do is create another data frame containing only the numerica variables of the original one.
On this website I found the solution of my problem, that is:
numeric_variables<-unlist(lapply(original_data,is.numeric))
X<-original_data[numeric_variables]
But I was wondering: why if I try like this, it does not work instead? what's wrong?
numeric_variables2<-apply(original_data,2,is.numeric)
x<-original_data[numeric_variables2]
try this :
names_num <- names(which(sapply(df, is.numeric)))
df_num <- df[, names_num]

Select rows from a data frame where any variable is not NA [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Removing empty rows of a data file in R
Suppose I have a dataframe df
I would like to select the rows from it, where any of the variables in the row are not NA. That is to say I only want to exclude the rows in which all the variables are NA
df[apply(!is.na(df), 1, any), ]

Resources