[R] Eliminating a row if something happens

David Winsemius dwinsemius at comcast.net
Sat Jul 2 00:10:12 CEST 2011


On Jul 1, 2011, at 12:14 PM, Trying To learn again wrote:

> Hi all,
>
> I want to create a new matrix based on a previous matrix.
>
> You see, If my "data" matrix accomplises this:
>
> if(rowSums(data[i,])>16|rowSums(data[i,])<28)
> data[i,]= data[i,]

'if' is not the right command.

> # if sum of rows is more than 16 and less 28 I conservate the row
> #but How I say if else "remove" the line (I tried this) but doesn´t
> works...I´m really a asn

Use logical indexing. (Using 'dat' because 'data' is bad practice as  
an object name.)

dat2 <- dat[ rowSums(dat) >16 | rowSums(dat) < 28 , ]

>
> else if(data[i,]=data[-i,])

The i- else construct only works on scalars. If you give them vectors  
they only use the first value and then they warn. You can use ifelse  
on vectors.


>
> You see imagine my data matrix is 10x4 and only 3 rows passes the
> condition...the resulting matrix will be 3x4
>
> I tried also to save the new matrix as csv like this
>
> write.csv(data, file = "input1.csv")
>
> How should I proceed to save as a txt?
>
> Many thanks I really like this problems but I feel my mind is  
> restricted to
> more easy things¡¡¡jajaj
>
> 	[[alternative HTML version deleted]]

If you are "trying to learn" then try to learn to post in plain text.

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list