Is there a way to delete NA in a location column? [duplicate] - r

This question already has answers here:
Omit rows containing specific column of NA
(10 answers)
Closed 1 year ago.
I have a data set of car crash data where I am gonna analyse them based of their locations. However, I want to clean the data first, How would I go about in removing crashes that have NA in the region column.

library(dplyr)
your_data_frame %>%
filter(!is.na(region_column))

Related

How to delete rows in r [duplicate]

This question already has answers here:
How do I delete rows in a data frame?
(10 answers)
Closed 1 year ago.
So I've been trying to subset and remove the observations of a country from my data frame (ESS6). I have been able to remove certain variables with this function, -c(variable), but that is not useful since I only want to remove certain rows from the variable countries (cntry).
Thank you for your help :)
Try using dplyr and the "filter" function

How do I subset a dataframe's columns if the data is all the same? [duplicate]

This question already has answers here:
How to remove columns with same value in R
(4 answers)
Closed 2 years ago.
I have a really large dataset and I want to filter out some of the columns because it is the same data all throughout (ex: company name is all "Walmart"). I can go through and do these manually but I'm looking for a code to do it automatically.
I had in mind a function to subset based on if sum(unique(colnam)) == 1 but not sure how to get it to work. Thanks.
which(sapply(dat, function(col) length(unique(col)) == 1))

Filtering Column by Multiple values [duplicate]

This question already has answers here:
Filter multiple values on a string column in dplyr
(6 answers)
Closed 2 years ago.
I would like to filter values based on one column with multiple values.
For example, one data.frame has s&p 500 tickers, i have to pick 20 of them and associated closing prices. How to do it?
If I understand well you question, I believe you should do it with dplyr:
library(dplyr)
target <- c("Ticker1", "Ticker2", "Ticker3")
filter(df, Ticker %in% target)
The answer can be found in https://stackoverflow.com/a/25647535/9513536
Cheers !

How can I delete the rows containing NA values in multiple columns using R? [duplicate]

This question already has answers here:
Remove rows with all or some NAs (missing values) in data.frame
(18 answers)
Closed 5 years ago.
Please view the image
Please view the attached image.I want to delete the rows containing NA in airsystemdelay,securitydelay,airlinedelay,lateaircraftdelay,waeatherdelay
Assuming you want to remove rows where any of columns 3 to 7 are NA:
df <- df[complete.cases(df[,c(3:7)]),]

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