Skip to content
Prev 333458 / 398506 Next

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

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,
Message-ID: <1385004033.8484.YahooMailNeo@web142604.mail.bf1.yahoo.com>
In-Reply-To: <CAGBvU3kFa2_B986YMOyWxWc_V6druk7BHFTi_gp4f5wXpKVXPw@mail.gmail.com>