This question already has answers here:
Remove rows with all or some NAs (missing values) in data.frame
(18 answers)
Closed 7 years ago.
I have a dataframe:
a<-c(1,2,3)
b<-c("CarA","CarB",NA)
data<-data.frame(a,b)
data
a b
1 1 CarA
2 2 CarB
3 3 NA
Now I want to remove the row with missing data (NA).
But this does not work:
data<-data[data[,2]!=NA,]
My thinking here is to look at the second column [,2] and look for those that don't have NA. Then extract the remaining data. Would someone be able to tell me what went wrong here?
Wouldn't
na.omit(data)
do? It seems the cleanest and fastest way to me.
By the way, your code does not work because you cannot do !=NA
Use is.na() instead (but na.omit() is better):
data[!is.na(data[,2]),]
If you want some other answer check complete.cases
data[complete.cases(data),]
Related
This question already has answers here:
How to filter data without losing NA rows using dplyr
(3 answers)
Closed 11 days ago.
I have a dataset (liver) where rows are patients and one column is how many days the patient waited between referral and assessment (referral_assessment_time).
I would like to filter the number of days patients waited between referral and assessment to greater than or equal to 0 days, but I keep losing the NAs despite trying some versions of rm.na = FALSE. I'd be very grateful for your help with how to keep the NAs.
liver <- liver %>% filter(referral_assessment_time >=0)
I think the question is different from "How to filter data without losing NA rows using dplyr" as that question seemed to be about strings and mine is about numbers and I can't apply that answer to my question. I'd be very grateful if #akrun reply could please be added back on here, many thanks!
An alternative would be to coalesce it:
data.frame(a=c(1, NA, 5)) %>%
filter(coalesce(a, Inf) > 3)
# a
# 1 NA
# 2 5
This question already has answers here:
How do I replace NA values with zeros in an R dataframe?
(29 answers)
Closed 1 year ago.
In a dataset of 589 rows there are around 50 entries who miss a variable and are assigned the NA value, but I know which value that should have ("Brussel Hoofstedelijk Gewest" is the exact string value it should have) so I want to give those 50 entries that string value and not change anything for the rest of the rows. I have search the internet high and low for this answer but I can't seem to find it, most problems people seem to have is with numerical values and NA and not with string values. I use Rstudio and mainly dplyr, tidy and ggplot2, this is an assignment for a introductory course on R so I am trying to learn it any help would be welcome :).
Have you tried:
My_df <- my_df %>%
mutate(column_of_interest=replace_na(column_of_interest, "Brussel Hoofstedelijk Gewest"))
?
This question already has answers here:
Remove columns from dataframe where some of values are NA
(8 answers)
Closed 3 years ago.
I have a 17(r) by 20 (c) matrix where all data is numbers and NA. I am trying to remove all columns that has the value NA in any rows. This is 11 of the 20 columns. I've been searching for an hour and tried several methods but couldn't get it right.
my.data [ ,!is.na(my.data[ ,1:20])]
To me this makes the most sense but is giving 'script too long' error.
One basic approach would be
mydata[, !is.na(colSums(mydata))]
This question already has answers here:
Removing NA observations with dplyr::filter()
(4 answers)
Closed 3 years ago.
I want to filter for rows that are NOT "NA" using the filter command. How do I do that?
Where "Dose_extract_IBUPROFEN" is the data frame, and "Drug3" is the variable for which a want to filter rows that are NOT missing (NA), I tried the following, which does not work.
filter(Dose_extract_IBUPROFEN, Drug3 != NA)
You should use !is.na(Drug3) instead of Drug3!=NA because you cannot use normal comparison operators with NA.
This question already has answers here:
Replace NA in column with value in adjacent column
(3 answers)
Closed 5 years ago.
I have problem with R like in a title. I have a data frame with 40+ rows and some kind of data in 2 columns I am interested in. What I want to do is to check if in column 2 is value or NA, and if there is NA copy value from the same row in column 1 (might be empty as well). I don't know exactly what approach should be used in this situation.
Regards,
RafaĆ
You can try using ifelse here:
df$col2 <- ifelse(is.na(df$col2), df$col1, df$col2)
This says, in English terms, to replace col2 with the value from col1 should the former be NA, otherwise leave the value as it was.