Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete certain rows if complete row contains zeros

Tags:

dataframe

r

Here is an example of my problem to mark my point.

 Random <- sample(c("A","B","C","D","E","F","G"), size = 100, replace = 
 TRUE)
 Year <- sample(c(2000,2001,2002,2003,2004,2005), 100, TRUE)
 Value <- sample(c(1,2,3,4), 100, TRUE)

 data <- data.frame(Random,Year,Value)

So what I want to do is to remove all the rows which do not change their values during the year in the #Table1 or at least give me back just the Rows from column Random in #Table2 that do so . I marked you the rows in this example I want to delete for a better understanding of my problem.

like image 300
bli12blu12 Avatar asked Jan 02 '23 10:01

bli12blu12


1 Answers

According to your logic, a row should be targeted for deletion if the value never changes. This condition can be phrased as being true when the minimum and maximum values are the same. Try this:

df <- data.frame(Random=c("A", "B", "C", "D", "E", "F", "G"),
                 `2000`=c(1,1,0,2,2,0,3),
                 `2001`=c(0,1,0,2,3,0,3),
                 `2002`=c(2,1,0,2,0,1,3),
                 `2003`=c(1,1,0,2,0,0,3),
                 `2004`=c(4,1,0,2,1,0,3),
                 `2005`=c(5,1,0,2,1,0,3), stringsAsFactors=FALSE)

df.target <- df[, !(names(df) %in% c("Random"))]
df[apply(df.target, 1, function(x) min(x)!=max(x)), ]

  Random X2000 X2001 X2002 X2003 X2004 X2005
1      A     1     0     2     1     4     5
5      E     2     3     0     0     1     1
6      F     0     0     1     0     0     0

Demo

Edit:

If you also want to delete the rows in table 1 whose names match rows which are being removed in the second table, you may try:

names.rm <- df$Random[apply(df.target, 1, function(x) min(x)==max(x))]
table1[!table1$Random %in% names.rm, ]
like image 179
Tim Biegeleisen Avatar answered Jan 17 '23 11:01

Tim Biegeleisen