Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R ,how can i replac the NA by the previous character [duplicate]

Tags:

r

na

there is a dataframe as blow(with NA values)

md <- data.frame(cat=c('a','b','d',NA,'E',NA),
                subcat=c('A','C',NA,NA,NA,'D')) 

 cat subcat
1    a      A
2    b      C
3    d   <NA>
4 <NA>   <NA>
5    E   <NA>
6 <NA>      D

i want to replace the NA by the previous character ,the result as below.

Using loop statement like 'for ...' can do it, but it's not that efficient . is there any formula or package can do it ? thanks!

  cat subcat
1   a      A
2   b      C
3   d      C
4   d      C
5   E      C
6   E      D
like image 259
anderwyang Avatar asked Jun 12 '21 00:06

anderwyang


People also ask

How do I replace Na in R?

The classic way to replace NA's in R is by using the IS.NA() function. The IS.NA() function takes a vector or data frame as input and returns a logical object that indicates whether a value is missing (TRUE or VALUE). Next, you can use this logical object to create a subset of the missing values and assign them a zero.

How do I replace NAs with 0 in R?

To replace NA with 0 in an R data frame, use is.na() function and then select all those values with NA and assign them to 0. myDataframe is the data frame in which you would like replace all NAs with 0.

How do you test for character 0 in R?

Many people forget that functions like str_locate_all() require %>% unlist() or %>% . [[1]] . Then you can easily detect character(0) with the length() function if > 0 one can safely use the output of str_locate_all() for example.

How to replace Na with specified values in R?

To replace NA with specified values in R, use the replace_na () function. The replace_na () function replaces NAs with specified values. We can replace it with 0 or any other value of our choice. replace_na (data, replace, ...) data: It is a data frame or Vector. replace: If the data is a Vector, the replace takes a single value.

How do I replace Na with specified values in plyr?

The dplyr package is the next iteration of plyr, focus on tools for working with data frames. The key object in dplyr is a tbl, a representation of a tabular data structure. To replace NA with specified values in R, use the replace_na () function. The replace_na () function replaces NAs with specified values.

How to deal with missing values in R?

A common way to treat missing values in R is to replace NA with 0. You will find a summary of the most popular approaches in the following. Choose one of these approaches according to your specific needs.

How to replace Na values in vector with Nonna values in Python?

library ("dplyr") df <- tibble (x = c (11, 21, NA), y = c ("x", NA, "y")) print (df) cat ("After replacing NAs", " ") df %>% tidyr::replace_na (list (x = "NonNA", y = "NonNA")) As you can see that we have replaced NA values with NonNA. You can use the replace_na () function to replace NA values in Vector.


2 Answers

You can use the na.locf function from the zoo package.

zoo::na.locf(md)
  cat subcat
1   a      A
2   b      C
3   d      C
4   d      C
5   E      C
6   E      D

Or use fill and everything from the tidyr and dplyr, respectively.

library(dplyr)
library(tidyr)

md %>% fill(everything())
#   cat subcat
# 1   a      A
# 2   b      C
# 3   d      C
# 4   d      C
# 5   E      C
# 6   E      D
like image 61
www Avatar answered Oct 09 '22 20:10

www


One approach is to use run length encoding rle(). Because it does not encode NAs, I replaced them with a string "NA".

roll_na <- function(.) {
  .[is.na(.)] <- "NA"
  var <- rle(.)
  na_ind <- which(var$values == "NA")
  var_lag <- c(NA, var$values[-length(var$values)])
  var$values[na_ind] <- var_lag[na_ind]
  
  rep(var$values, times = var$lengths)
}

library(dplyr)

md %>% 
  mutate(across(everything(), roll_na))

#   cat subcat
# 1   a      A
# 2   b      C
# 3   d      C
# 4   d      C
# 5   E      C
# 6   E      D
like image 42
Zaw Avatar answered Oct 09 '22 22:10

Zaw