!is.na Remove columns with NA value (in any rows) [duplicate] - r

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))]

Related

Get the average for each 4 rows [duplicate]

This question already has answers here:
How to get the sum of each four rows of a matrix in R
(3 answers)
Average of n rows
(1 answer)
Closed 8 months ago.
I have a column with hundreds of values, I want to get the average for each 4 rows, then I move to another four rows, etc. How could I write that loop in R studio, I attached here a simple example, what should I do if I have NA values, how to consider those values as zeros?

How can I remove all rows that have NA for a certain variable [duplicate]

This question already has answers here:
How to remove row if it has a NA value in one certain column [duplicate]
(4 answers)
Closed 2 years ago.
My dataset ('data') has 1719 cases and 6779 variables. I need to weight the data using variable 'weight', however this is missing for 69 cases.
How can I delete the rows that have NA in the weight column, without deleting variables that have NA in any of the other 6778 columns?
Index rows by columns containing NA
data[!is.na(data[,"weight"]),]
Data frames are indexed using square braces to specify rows then columns separated by a comma: data[rows, columns]
You can then provide a vector of rows, using the is.na function and preceeded by the exclamation mark, making it effectively an is.NOT.na.
!is.na(data[,"weight"])
From my 'useful R commands' file ....
# drop a row with a NA value in a cell
df <- df[ !is.na(df$variable), ]

replacing NA's in one column with the product of two other columns [duplicate]

This question already has answers here:
Replace NA in column with value in adjacent column
(3 answers)
Closed 4 years ago.
In a column of 7000 rows there are 11 NA's. I want to replace those NA's with the product of two other columns in my data frame
The column with NA's is TOTALCHARGES and the two columns I want to multiply are TENURE and MONTHLYCHARGES.
Find the indices of the missing data:
na.vals <- which(is.na(your_data$TOTALCHARGES))
Modify the relevant elements of TOTALCHARGES (within the data set):
your_data <- transform(your_data,
TOTALCHARGES=replace(TOTALCHARGES,na.vals,
TENURE[na.vals]*MONTHLYCHARGES[na.vals]))
Something like this (assuming df is your data.frame)?
df[is.na(df$TOTALCHARGES), "TOTALCHARGES"] <- df[is.na(df$TOTALCHARGES), "TENURE"] * df[is.na(df$TOTALCHARGES), "MONTHLYCHARGES"]

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