Here’s a data table approach that uses na.omit()
across the columns, grouped by ID.
library(data.table)
setDT(df)[, lapply(.SD, na.omit), by = ID]
# ID Col1 Col2 Col3 Col4
# 1: 1 5 10 15 20
# 2: 2 25 30 35 40
More Related Contents:
- aggregate methods treat missing values (NA) differently
- How can I count the number of NAs per group?
- Aggregate / summarize multiple variables per group (e.g. sum, mean)
- Collapse / concatenate / aggregate a column to a single comma separated string within each group
- Extract row corresponding to minimum value of a variable by group
- Group by multiple columns and sum other multiple columns
- Select the first and last row by group in a data frame
- Replacing character values with NA in a data frame
- Apply function conditionally
- How to delete rows from a dataframe that contain n*NA
- Aggregate multiple columns at once [duplicate]
- Aggregate a dataframe on a given column and display another column
- Compute mean and standard deviation by group for multiple variables in a data.frame
- How to avoid warning when introducing NAs by coercion
- Remove columns from dataframe where some of values are NA
- How to subset data in R without losing NA rows?
- Group integer vector into consecutive runs
- How to delete columns that contain ONLY NAs?
- Randomly insert NAs into dataframe proportionaly
- Apply several summary functions (sum, mean, etc.) on several variables by group in one call
- Aggregate data in R
- Replace a value NA with the value from another column in R
- Replace in a factor column
- Counting non NAs in a data frame; getting answer as a vector
- R use ddply or aggregate
- R: replacing NAs in a data.frame with values in the same position in another dataframe
- Run a custom function on a data frame in R, by group
- Move NAs to the end of each column in a data frame
- Filtering rows in R unexpectedly removes NAs when using subset or dplyr::filter
- Count number of non-NA values by group