Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I extract this data that I want in R?

I have a data called "test"

   dput(test)>
   structure(list(Data = c("Apple", "3", "Orange", "4", "Grape", 
   "2", "Orange", "5", "Grape", "1", "Orange", "4")), class = c("tbl_df", 
   "tbl", "data.frame"), row.names = c(NA, -12L))

I wish to extract the "Orange" number. How do I do that if all my data is in one column.

My desire output is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 Data  

 Orange1
  4         
 Orange2
  5     
 Orange3
  4     



  

>Solution :

With dplyr library:

library(dplyr)

test%>% filter(Data == "Orange" | lag(Data)=="Orange")

Output:

# A tibble: 6 × 1
  Data  
  <chr> 
1 Orange
2 4     
3 Orange
4 5     
5 Orange
6 4 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading