dimanche 6 mai 2018

r delete certain rows if complete row contains zeros

I got the following question I cant not figure out yet.

I got the following example 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)

 # open dplyr library
 library(dplyr)

 #Group data by Random and year and count Values that are not NA 
 data %>% group_by(Random, Year) %>% summarise(count =    
 length(Value[!is.na(Value)]))

Table1

       Random  Year count
       <fctr>   <dbl> <int>
  1       A  2004     3
  2       C  2003     3
  3       A  2004     1
  4       A  2003     1
  5       C  2005     3
  ....

Then I change my table to the following pivottable with

  pivottable <- xtabs(count ~ Random + year, data)
  pivottable

which gives me the following format

Table 2

     year   2000 2001  2002 2003 2004 2005 
  Random  
    A        1    0      2   1    4    5     
    B        1    1      1   1    1    1     <--- This one
    C        0    0      0   0    0    0     <--- This one
    D        2    2      2   2    2    2     <--- This one
    E        2    3      0   0    1    1
    F        0    0      1   0    0    0
    G        3    3      3   3    3    3     <--- This one

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.

I would really appreciate your help on this :) Thanks!

Aucun commentaire:

Enregistrer un commentaire