[R] conditional filter resulting in 2 new dataframes

Dennis Murphy djmuser at gmail.com
Sun Aug 14 23:37:27 CEST 2011


Hi:

Use replace():

replace(initial, initial < 5, 0)

     sample1 sample2 sample3
1900       0       8       0
1901       5       6       5
1902       0       0       0
1903       8       0       0
1904       0       7       0
1905       0       5       6

replace(initial, initial >= 5, 0)

     sample1 sample2 sample3
1900       1       0       4
1901       0       0       0
1902       4       4       3
1903       0       2       2
1904       4       0       4
1905       4       0       0

HTH,
Dennis

On Sat, Aug 13, 2011 at 6:02 PM, andrewjt <atrant at mun.ca> wrote:
> This is what I am starting with:
>
> initial<- matrix(c(1,5,4,8,4,4,8,6,4,2,7,5,4,5,3,2,4,6), nrow=6,
> ncol=3,dimnames=list(c("1900","1901","1902","1903","1904","1905"),
> c("sample1","sample2","sample3")))
>
> And I need to apply a filter (in this case, any value <5) to give me one
> dataframe with only the 'less than 5' values and with '0' place holders on
> the values that don't meet this criteria. The second dataframe is the same
> but for those values >=5.
>
> Should look like this:
> less <- matrix(c(1,0,4,0,4,4,0,0,4,2,0,0,4,0,3,2,4,0), nrow=6,
> ncol=3,dimnames=list(c("1900","1901","1902","1903","1904","1905"),
> c("sample1","sample2","sample3")))
> more <- matrix(c(0,5,0,8,0,0,8,6,0,0,7,5,0,5,0,0,0,6), nrow=6,
> ncol=3,dimnames=list(c("1900","1901","1902","1903","1904","1905"),
> c("sample1","sample2","sample3")))
>
> Any ideas?
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/conditional-filter-resulting-in-2-new-dataframes-tp3742232p3742232.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list