Could also try using length<-
df[] <- t(apply(df, 1, function(x) `length<-`(na.omit(x), length(x))))
df
# X1 X2 X3 X4
# x1 a b c <NA>
# x2 d e <NA> <NA>
# x3 f g h i
# x4 j k l <NA>
More Related Contents:
- Reshaping data.frame from wide to long format
- Quickly reading very large tables as dataframes
- Reshape three column data frame to matrix (“long” to “wide” format) [duplicate]
- Combine two data frames by rows (rbind) when they have different sets of columns
- Counting unique / distinct values by group in a data frame
- How to combine multiple conditions to subset a data-frame using “OR”?
- cbind a dataframe with an empty dataframe – cbind.fill?
- Split a large dataframe into a list of data frames based on common value in column
- Conditional replacement of values in a data.frame
- Convert data.frame column format from character to factor
- How to convert a table to a data frame
- How to swap values between two columns
- Define and apply custom bins on a dataframe
- Subset of rows containing NA (missing) values in a chosen column of a data frame
- rbind dataframes with a different column name
- Find duplicated rows (based on 2 columns) in Data Frame in R
- Converting two columns of a data frame to a named vector
- Collapsing data frame by selecting one row per group
- How to initialize empty data frame (lot of columns at the same time) in R
- Practical limits of R data frame
- R: Assign variable labels of data frame columns
- rbind data frames based on a common pattern in data frame name
- Select last non-NA value in a row, by row
- Join two data frames in R based on closest timestamp
- How to append a whole dataframe to a CSV in R
- Convert a data frame to a data.table without copy
- How can I merge more than 2 dataframes in R by rownames?
- Shifting a column down by one
- Identifying where value changes in R data.frame column
- What is the first column in R’s mtcars dataset?