wawawa
wawawa

Reputation: 3345

deleting rows without filtering other values

Say I have a dataframe like this:

NAME    YEAR      PERCENTAGE
A       2001        NA
A       2002        NA
A       2003        5.0
B       2001        3.3 
B       2002        2.3 
B       2003        NA 

I want remove rows with NA by selecting the specific rows:

NAME    YEAR      PERCENTAGE
A       2003        5.0
B       2001        3.3 
B       2002        2.3 

and then change B to A,expected output like this:

NAME    YEAR      PERCENTAGE
A       2001        3.3 
A       2002        2.3 
A       2003        5.0

I tried subset(),but since I have other values, it would filter other values which should be remained.

Upvotes: 1

Views: 49

Answers (2)

prosoitos
prosoitos

Reputation: 7327

Assuming your data frame is called df:

library(dplyr)

df %>% na.omit() %>% mutate(NAME = "A")

Result:

  NAME YEAR PERC
1    A 2003  5.0
2    A 2001  3.3
3    A 2002  2.3

Upvotes: 1

demarsylvain
demarsylvain

Reputation: 2185

With the library dplyr, you have access to several functions (like filter(), arrange() or mutate()) that able you to modify you dataframe:

# the dataframe    
df <- data.frame(
      NAME = rep(c('A', 'B'), each = 3),
      YEAR = rep(2001:2003, length = 6),
      PERC = c(NA, NA, 5, 3.3, 2.3, NA)
    )

# load the library
library(dplyr)


df %>% 
  filter(!is.na(PERC)) %>%                         # filter missing values
  arrange(YEAR) %>%                                # order according YEAR
  mutate(NAME = replace(NAME, NAME == 'B', 'A'))   # change values

# result
  NAME YEAR PERC
1    A 2001  3.3
2    A 2002  2.3
3    A 2003  5.0

Upvotes: 4

Related Questions