How can I delete both rows and columns that do not meet a particular cut off value. Example:> d <- rbind(c(0, 1, 6, 4),+ c(2, 5, 7, 5), + c(3, 6, 1, 6), + c(4, 4, 4, 4))> f <- as.matrix(d) > f[,1] [,2] [,3] [,4] [1,] 0 1 6 4 [2,] 2 5 7 5 [3,] 3 6 1 6 [4,] 4 4 4 4 I would like to delete all rows and columns that do not contain at least one element with a value less than 1. So I'd end up with:> f[,1] [,2] [,3] [1,] 0 1 6 [3,] 3 6 1 Note: 1 is an arbitrary cut-off value. [[alternative HTML version deleted]]
f[rowSums(f<=1)>0,colSums(f<=1)>0] Judging from your result, you want "less than or equal to 1". HTH, Stephan Crosby, Jacy R schrieb:> How can I delete both rows and columns that do not meet a particular cut off value. > Example: >> d <- rbind(c(0, 1, 6, 4), > + c(2, 5, 7, 5), > + c(3, 6, 1, 6), > + c(4, 4, 4, 4)) >> f <- as.matrix(d) >> f > [,1] [,2] [,3] [,4] > [1,] 0 1 6 4 > [2,] 2 5 7 5 > [3,] 3 6 1 6 > [4,] 4 4 4 4 > > I would like to delete all rows and columns that do not contain at least one element with a value less than 1. So I'd end up with: > >> f > [,1] [,2] [,3] > [1,] 0 1 6 > [3,] 3 6 1 > > Note: 1 is an arbitrary cut-off value. > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
On 22/04/2009, at 8:34 AM, Crosby, Jacy R wrote:> How can I delete both rows and columns that do not meet a > particular cut off value. > Example: >> d <- rbind(c(0, 1, 6, 4), > + c(2, 5, 7, 5), > + c(3, 6, 1, 6), > + c(4, 4, 4, 4)) >> f <- as.matrix(d) >> f > [,1] [,2] [,3] [,4] > [1,] 0 1 6 4 > [2,] 2 5 7 5 > [3,] 3 6 1 6 > [4,] 4 4 4 4 > > I would like to delete all rows and columns that do not contain at > least one element with a value less than 1.Apparently you actually want ``less than or equal to 1''.> So I'd end up with: > >> f > [,1] [,2] [,3] > [1,] 0 1 6 > [3,] 3 6 1 > > Note: 1 is an arbitrary cut-off value.d[apply(d,1,function(x){any(x<=1)}),apply(d,2,function(x){any(x<=1)})] cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Crosby, Jacy R
2009-Apr-22 16:44 UTC
[R] Deleting rows that contain certain characters...or deleting ANOVA factors
I'm hoping there's an easier way to do this... I have a large matrix which looks like this (data=REG, rownames are the Pat1, Pat2,...): L1 L2 L3 Phen1 Phen2 Phen3 Pat1 AA TT A? 143 143 143 Pat2 AT ?A AT 256 256 NA Pat3 TA ?? TA 212 212 212 Pat4 TT ?T AA NA 220 220 Pat5 T? A? AT 98 98 98 Pat6 ?A AA TA 113 113 113 Pat7 ?? ?? TT 178 178 178 Pat8 ?T ?T T? 200 NA 200 Pat9 A? A? A? 120 120 120 Pat10 AA AA T? 230 230 230 I'm performing an ANOVA: for(i in 1:3){ for(j in 1:3){ x<-summary(aov(REG[,3+j]~REG[,i], data=REG, na.act=na.exclude))[[1]][,5] #summary()[[1]][,5] to extract p-value aov1[i,j]<-x[-2] #removes the second element of pvalue...an NA } } This works fine and returns a p-value for each Phen~L model. However, I'd like to exclude cases where there is a '?' in L. I do not need these for the analysis. i.e. I'd like to have aov(Phen1~L1) use only Pat1-Pat4,and Pat 10. Similarly, aov(Phen1~L2) should use Pat1, 6, and 10. Etc. Is this something I can do in the aov function, or do I need to modify my dataset before running aov? In either case, I need ideas...
Apparently Analagous Threads
- Recoding dates to session id in a longitudinal dataset
- Extract some character from a character vector of length 1
- number of patients in a hospital on a given date
- how to edit my R codes into a efficient way
- In-string variable/symbol substitution: What formats/syntax is out there?