[R] How to extract sets of rows (not sorted) from text file in R, do some methods on these rows, save the result in another text file, then pick others set of rows and do the same

arun smartpink111 at yahoo.com
Thu Nov 21 04:20:33 CET 2013


Hi,
dat1 <- read.csv("Manal.csv",header=FALSE) 
str(dat1)
#'data.frame':    31 obs. of  9 variables:
# $ V1: int  1 1 1 1 3 1 2 2 3 2 ...
# $ V2: int  1 1 0 0 0 1 0 0 0 0 ...
# $ V3: int  0 0 1 0 0 0 0 0 0 0 ...
# $ V4: int  1 1 0 0 0 0 0 0 0 0 ...
# $ V5: int  0 0 1 0 1 0 0 1 1 0 ...
# $ V6: int  0 0 0 0 0 0 0 0 0 0 ...
# $ V7: int  1 0 0 0 0 0 0 0 0 0 ...
# $ V8: int  0 1 0 0 0 0 0 0 0 0 ...
# $ V9: int  0 0 1 0 1 0 0 0 0 0 ...
res <- do.call(rbind,lapply(split(dat1[,-1],dat1[,1]),function(x) (colSums(x)>= nrow(x)/2)*1))
dim(res)
#[1] 10  8


res[1:2,] ## this is the result you showed in the excel sheet
  V2 V3 V4 V5 V6 V7 V8 V9
#1  1  0  0  0  0  0  0  0
#2  0  0  0  0  0  0  0  0

Then, you can use ?write.table() as showed previously.


A.K.







On Wednesday, November 20, 2013 7:27 PM, Manal H <manal76 at gmail.com> wrote:

Hi
First thanks a lot for answering me. sorry if I wasn't clear, English is not my first language. I tried to use your code but it didn't give what I want. I'm new in r, so sorry for being annoying.

I have attached a  reproducible example 


thanks and best




On Tue, Nov 19, 2013 at 1:55 PM, <smartpink111 at yahoo.com> wrote:

Hi,
>
>I am not sure I understand you completely.  Are you applying the same set of methods on each of the groups?  If yes, then the methods I showed should work.  If it is not the case, please provide a reproducible example.
>Thanks.
>A.K.
>
><quote author='memi'>
>
>Thanks a lot arun kirshna , but my question was:
>
>I have this input data
>1 2 4 7
>1 4 5 8
>1 4 5 6
>2 3 4 8
>3 6 7 8
>1 5 6 9
>2 5 7 9
>3 7 9 11
>
>I want the output like:
>
>step1:  from the input data, extract the rows that start with one
>after I do this step,
>I got the following rows: (I don't want extract the first column that
>represents the group number)
> 2 4 7
> 4 5 8
> 4 5 6
> 5 6 9
>
>step1.1: apply some methods in the extracted data, then save the result in
>new text file.
>-----
>
>
>step2:   from the input data, extract the rows that start with two
>after I do this step,
>I got the following rows: (I don't want extract the first column that
>represents the group number)
> 3 4 8
> 5 7 9
>
>step2.1: apply some methods in the extracted data, then save the result
>after the result in step one.
>----
>
>step3:  from the input data, extract the rows that start with three
>after I do this step,
>I got the following rows: (I don't want extract the first column that
>represents the group number)
> 6 7 8
> 7 9 11
>
>step3.1: apply some methods in the extracted data, then save the result
>after the result in step two.
>-----
>
>and so on
>my input txt file has more than 9000 rows, each row start with group number,
>these number from 1 to 100
>so I want do the process 100 times (from  1 to 100)
> - extract the rows that start with 1, apply methods on them, save the
>result in new txt file
>-  extract the rows that start with 2, apply methods on them, save the
>result in new txt file after the previous result.
>-  extract the rows that start with 3, apply methods on them, save the
>result in new txt file after the previous result.
>-  extract the rows that start with 4, apply methods on them, save the
>result in new txt file after the previous result.
>
>.
>.
>-  extract the rows that start with 100, apply methods on them, save the
>result in new txt file after the previous result.
>
>
>
></quote>
>Quoted from:
>http://r.789695.n4.nabble.com/Re-How-to-extract-sets-of-rows-not-sorted-from-text-file-in-R-do-some-methods-on-these-rows-save-thee-tp4680741p4680759.html
>
>
>_____________________________________
>Sent from http://r.789695.n4.nabble.com
>
>



More information about the R-help mailing list