deleting rows without filtering other values












1















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.










share|improve this question




















  • 3





    What about df[!is.na(df$PERCENTAGE),] ?

    – CIAndrews
    Nov 29 '18 at 0:34






  • 1





    Do you need to order the output by YEAR? You don't mention it, but this is the case in your expected output

    – prosoitos
    Nov 29 '18 at 0:50











  • ordering by name and then order the year of each name

    – wawawa
    Nov 29 '18 at 0:52
















1















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.










share|improve this question




















  • 3





    What about df[!is.na(df$PERCENTAGE),] ?

    – CIAndrews
    Nov 29 '18 at 0:34






  • 1





    Do you need to order the output by YEAR? You don't mention it, but this is the case in your expected output

    – prosoitos
    Nov 29 '18 at 0:50











  • ordering by name and then order the year of each name

    – wawawa
    Nov 29 '18 at 0:52














1












1








1








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.










share|improve this question
















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.







r dplyr subset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 1:02









Ronak Shah

44.8k104266




44.8k104266










asked Nov 29 '18 at 0:28









wawawawawawa

587




587








  • 3





    What about df[!is.na(df$PERCENTAGE),] ?

    – CIAndrews
    Nov 29 '18 at 0:34






  • 1





    Do you need to order the output by YEAR? You don't mention it, but this is the case in your expected output

    – prosoitos
    Nov 29 '18 at 0:50











  • ordering by name and then order the year of each name

    – wawawa
    Nov 29 '18 at 0:52














  • 3





    What about df[!is.na(df$PERCENTAGE),] ?

    – CIAndrews
    Nov 29 '18 at 0:34






  • 1





    Do you need to order the output by YEAR? You don't mention it, but this is the case in your expected output

    – prosoitos
    Nov 29 '18 at 0:50











  • ordering by name and then order the year of each name

    – wawawa
    Nov 29 '18 at 0:52








3




3





What about df[!is.na(df$PERCENTAGE),] ?

– CIAndrews
Nov 29 '18 at 0:34





What about df[!is.na(df$PERCENTAGE),] ?

– CIAndrews
Nov 29 '18 at 0:34




1




1





Do you need to order the output by YEAR? You don't mention it, but this is the case in your expected output

– prosoitos
Nov 29 '18 at 0:50





Do you need to order the output by YEAR? You don't mention it, but this is the case in your expected output

– prosoitos
Nov 29 '18 at 0:50













ordering by name and then order the year of each name

– wawawa
Nov 29 '18 at 0:52





ordering by name and then order the year of each name

– wawawa
Nov 29 '18 at 0:52












2 Answers
2






active

oldest

votes


















4














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





share|improve this answer





















  • 1





    These are all dplyr functions, so just library(dplyr) is enough.

    – neilfws
    Nov 29 '18 at 0:41











  • you're right. I will ajust the post.

    – demarsylvain
    Nov 29 '18 at 0:45



















1














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





share|improve this answer



















  • 1





    This works for the example data, but it would (a) omit rows that are NA in any column and (b) change any name to "A", not just "B" to "A".

    – neilfws
    Nov 29 '18 at 0:50






  • 1





    It is unclear in the question whether those are requirements or not

    – prosoitos
    Nov 29 '18 at 0:51











  • Note that I agree with you :) and the other answer is much more general (+1). But I went with the minimal code given the info in the question

    – prosoitos
    Nov 29 '18 at 0:55












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53530128%2fdeleting-rows-without-filtering-other-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














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





share|improve this answer





















  • 1





    These are all dplyr functions, so just library(dplyr) is enough.

    – neilfws
    Nov 29 '18 at 0:41











  • you're right. I will ajust the post.

    – demarsylvain
    Nov 29 '18 at 0:45
















4














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





share|improve this answer





















  • 1





    These are all dplyr functions, so just library(dplyr) is enough.

    – neilfws
    Nov 29 '18 at 0:41











  • you're right. I will ajust the post.

    – demarsylvain
    Nov 29 '18 at 0:45














4












4








4







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 0:46

























answered Nov 29 '18 at 0:39









demarsylvaindemarsylvain

744214




744214








  • 1





    These are all dplyr functions, so just library(dplyr) is enough.

    – neilfws
    Nov 29 '18 at 0:41











  • you're right. I will ajust the post.

    – demarsylvain
    Nov 29 '18 at 0:45














  • 1





    These are all dplyr functions, so just library(dplyr) is enough.

    – neilfws
    Nov 29 '18 at 0:41











  • you're right. I will ajust the post.

    – demarsylvain
    Nov 29 '18 at 0:45








1




1





These are all dplyr functions, so just library(dplyr) is enough.

– neilfws
Nov 29 '18 at 0:41





These are all dplyr functions, so just library(dplyr) is enough.

– neilfws
Nov 29 '18 at 0:41













you're right. I will ajust the post.

– demarsylvain
Nov 29 '18 at 0:45





you're right. I will ajust the post.

– demarsylvain
Nov 29 '18 at 0:45













1














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





share|improve this answer



















  • 1





    This works for the example data, but it would (a) omit rows that are NA in any column and (b) change any name to "A", not just "B" to "A".

    – neilfws
    Nov 29 '18 at 0:50






  • 1





    It is unclear in the question whether those are requirements or not

    – prosoitos
    Nov 29 '18 at 0:51











  • Note that I agree with you :) and the other answer is much more general (+1). But I went with the minimal code given the info in the question

    – prosoitos
    Nov 29 '18 at 0:55
















1














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





share|improve this answer



















  • 1





    This works for the example data, but it would (a) omit rows that are NA in any column and (b) change any name to "A", not just "B" to "A".

    – neilfws
    Nov 29 '18 at 0:50






  • 1





    It is unclear in the question whether those are requirements or not

    – prosoitos
    Nov 29 '18 at 0:51











  • Note that I agree with you :) and the other answer is much more general (+1). But I went with the minimal code given the info in the question

    – prosoitos
    Nov 29 '18 at 0:55














1












1








1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 29 '18 at 0:47









prosoitosprosoitos

935520




935520








  • 1





    This works for the example data, but it would (a) omit rows that are NA in any column and (b) change any name to "A", not just "B" to "A".

    – neilfws
    Nov 29 '18 at 0:50






  • 1





    It is unclear in the question whether those are requirements or not

    – prosoitos
    Nov 29 '18 at 0:51











  • Note that I agree with you :) and the other answer is much more general (+1). But I went with the minimal code given the info in the question

    – prosoitos
    Nov 29 '18 at 0:55














  • 1





    This works for the example data, but it would (a) omit rows that are NA in any column and (b) change any name to "A", not just "B" to "A".

    – neilfws
    Nov 29 '18 at 0:50






  • 1





    It is unclear in the question whether those are requirements or not

    – prosoitos
    Nov 29 '18 at 0:51











  • Note that I agree with you :) and the other answer is much more general (+1). But I went with the minimal code given the info in the question

    – prosoitos
    Nov 29 '18 at 0:55








1




1





This works for the example data, but it would (a) omit rows that are NA in any column and (b) change any name to "A", not just "B" to "A".

– neilfws
Nov 29 '18 at 0:50





This works for the example data, but it would (a) omit rows that are NA in any column and (b) change any name to "A", not just "B" to "A".

– neilfws
Nov 29 '18 at 0:50




1




1





It is unclear in the question whether those are requirements or not

– prosoitos
Nov 29 '18 at 0:51





It is unclear in the question whether those are requirements or not

– prosoitos
Nov 29 '18 at 0:51













Note that I agree with you :) and the other answer is much more general (+1). But I went with the minimal code given the info in the question

– prosoitos
Nov 29 '18 at 0:55





Note that I agree with you :) and the other answer is much more general (+1). But I went with the minimal code given the info in the question

– prosoitos
Nov 29 '18 at 0:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53530128%2fdeleting-rows-without-filtering-other-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)