R how to retrieve data from data.frame with multiple conditions

CodeGeek123

I am wondering how to perform some basic data manipulation in R. What i want to do is the following.

I have a data table with the following pattern :

  V1     V2     V3    
  ABC     X     24
  ABC     Y     30
  EFG     X     4
  EFG     Y     28
  HIJ     P     40
  HIJ     Y     41
  PKL     X     32
  1. Now i want to retrieve all the values/pairs of V1 where it doesn't have a corresponding value which is not X on V2. In the above dataset this subset would be

    HIJ     P    40
    HIJ     Y    41
    

Since neither of the pair of HIJ have a V2 value of X.

  1. I would also like to retrieve all values of V1 that don't repeat twice. In the above example it would be

    PKL  X 32
    
David Arenburg

You mentioned data.table, so here's two possible approaches for both requests

library(data.table)

For 1.

setDT(df)[, .SD[all(V2 != "X")], by = V1]
#     V1 V2 V3
# 1: HIJ  P 40
# 2: HIJ  Y 41

For 2.

df[, .SD[.N == 1L], by = V1]
#     V1 V2 V3
# 1: PKL  X 32

Or (a bit more optimized version)

indx <- df[, .(indx = .I[.N == 1L]), by = V1]$indx
df[indx]
#     V1 V2 V3
# 1: PKL  X 32

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R how to retrieve data from data.frame with multiple conditions

From Dev

How to retrieve value from a data frame "between" two conditions

From Dev

How to retrieve and store multiple values from a python Data Frame?

From Dev

R: find data frame index of multiple conditions

From Dev

SQL Server query to retrieve data from two tables with multiple conditions

From Dev

Count based on multiple conditions from other data.frame

From Dev

Selecting values from pandas data frame using multiple conditions

From Dev

How to retrieve data by following conditions?

From Dev

how to retrieve data from multiple JTextField

From Dev

how to retrieve data from data frame 1 contents that do not have in data frame 2 in Scala

From Dev

R how to filter a data frame based on conditions in following rows?

From Dev

Filter data frame based on multiple conditions in another data frame

From Dev

Subsetting a data frame based on another data frame with multiple conditions

From Dev

how can I retrieve changes to data frame edits from gdf

From Dev

create a multiple data frames from a single data frame in r

From Dev

How to create a raster from a data frame in r?

From Dev

replace data frame values in R with double conditions

From Dev

Retrieve data from multiple models

From Dev

Retrieve data from multiple arrays

From Dev

Retrieve data from multiple models

From Dev

R: convert data from wide to long - multiple conditions - getting error

From Dev

Nested function to retrieve data from multiple URLs (with authentication) in R

From Dev

I want to generate 8 combinations of names from a column in an R data frame based on conditions from other columns in the same data frame

From Dev

I want to generate 8 combinations of names from a column in an R data frame based on conditions from other columns in the same data frame

From Dev

how to filter data frame with conditions of two columns?

From Dev

How to replace values in multiple columns in a data.frame with values from a vector in R?

From Dev

How to output multiple pdf files from many data frame in a for loop in R

From Dev

How to replace values in multiple columns in a data.frame with values from a vector in R?

From Dev

How to read multiple files and create a single data frame from them in R?

Related Related

  1. 1

    R how to retrieve data from data.frame with multiple conditions

  2. 2

    How to retrieve value from a data frame "between" two conditions

  3. 3

    How to retrieve and store multiple values from a python Data Frame?

  4. 4

    R: find data frame index of multiple conditions

  5. 5

    SQL Server query to retrieve data from two tables with multiple conditions

  6. 6

    Count based on multiple conditions from other data.frame

  7. 7

    Selecting values from pandas data frame using multiple conditions

  8. 8

    How to retrieve data by following conditions?

  9. 9

    how to retrieve data from multiple JTextField

  10. 10

    how to retrieve data from data frame 1 contents that do not have in data frame 2 in Scala

  11. 11

    R how to filter a data frame based on conditions in following rows?

  12. 12

    Filter data frame based on multiple conditions in another data frame

  13. 13

    Subsetting a data frame based on another data frame with multiple conditions

  14. 14

    how can I retrieve changes to data frame edits from gdf

  15. 15

    create a multiple data frames from a single data frame in r

  16. 16

    How to create a raster from a data frame in r?

  17. 17

    replace data frame values in R with double conditions

  18. 18

    Retrieve data from multiple models

  19. 19

    Retrieve data from multiple arrays

  20. 20

    Retrieve data from multiple models

  21. 21

    R: convert data from wide to long - multiple conditions - getting error

  22. 22

    Nested function to retrieve data from multiple URLs (with authentication) in R

  23. 23

    I want to generate 8 combinations of names from a column in an R data frame based on conditions from other columns in the same data frame

  24. 24

    I want to generate 8 combinations of names from a column in an R data frame based on conditions from other columns in the same data frame

  25. 25

    how to filter data frame with conditions of two columns?

  26. 26

    How to replace values in multiple columns in a data.frame with values from a vector in R?

  27. 27

    How to output multiple pdf files from many data frame in a for loop in R

  28. 28

    How to replace values in multiple columns in a data.frame with values from a vector in R?

  29. 29

    How to read multiple files and create a single data frame from them in R?

HotTag

Archive