Here are what i am going to accomplish: I have 400 files named as xxx.txt. the content of the file looks like the following: name count 1. aaa 100 2. bbb 2000 3. ccc 300 4. ddd 3000 ........ more that 1000 rows in each files. these are the areas i need help: 1. how can i only read in the files with the string patterns ggg or fff as part of the file names? for instance, I only need the file names with the ggg or fff in it xxxxx_ggg_yyyyy_1.txt yyyy_fff_yyyy_xxx.txt i don't need to read in the files, such as xxxx_aaa_yyyy.txt 2.how cam rename the files: for instance: xxxxx_ggg_yyyyy_1.txt======>changed to ggg1a.txt 3.after the files read in, how can i only keep the rows with the aaa and bbb, everything elses show be removed from the files, but the files still remain the same file name? for instance, in the xxxxx_ggg_yyyyy_1.txt file, it shouls looks like: name count 1. aaa 100 2. bbb 2000 3. aaa 300 4. bbb 400 Thanks so lot, I am very new to R, I am looking forward to any helps from you. -- View this message in context: http://r.789695.n4.nabble.com/how-to-process-this-in-R-tp2281283p2281283.html Sent from the R help mailing list archive at Nabble.com.
Hi, You might want to check out ?system ?read.table and ?subset and also ?sub and maybe ?strsplit Reading one of the good intro to R documents out there would also help you get started with this fairly straightforward task Beyond that, you did a good job of explaining your problem, but this is one of the cases where knowing your OS would be awfully useful. Sarah On Wed, Jul 7, 2010 at 1:49 PM, jd6688 <jdsignature at gmail.com> wrote:> > Here are what i am going to accomplish: > > I have 400 files named as xxx.txt. the content of the file looks like the > following: > > ? ?name ? ?count > > 1. aaa ? ? 100 > 2. bbb ? ?2000 > 3. ccc ? ?300 > 4. ddd ? 3000 > > ........ > more that 1000 rows in each files. > > these are the areas i need help: > 1. how can i only read in the files with the string patterns ggg or fff as > part of the file names? > ?for instance, I only need the file names with the ggg or fff in it > ? ? xxxxx_ggg_yyyyy_1.txt > ? ? yyyy_fff_yyyy_xxx.txt > > ? ?i don't need to read in the files, such as xxxx_aaa_yyyy.txt > > 2.how cam rename the files: > > ?for instance: xxxxx_ggg_yyyyy_1.txt======>changed to ggg1a.txt > > > 3.after the files read in, how can i only keep the rows with the aaa and > bbb, everything elses show be removed from the files, but the files still > remain the same file name? > > ? for instance, in the xxxxx_ggg_yyyyy_1.txt file, it shouls looks like: > ?name ? ?count > > 1. aaa ? ?100 > 2. bbb ? ?2000 > 3. aaa ? ?300 > 4. bbb ? ?400 > > > Thanks so lot, I am very new to R, I am looking forward to any helps from > you. > > ---- Sarah Goslee http://www.functionaldiversity.org
Hi On Wed, Jul 7, 2010 at 7:49 PM, jd6688 <jdsignature@gmail.com> wrote:> > Here are what i am going to accomplish: > > I have 400 files named as xxx.txt. the content of the file looks like the > following: > > name count > > 1. aaa 100 > 2. bbb 2000 > 3. ccc 300 > 4. ddd 3000 > > ........ > more that 1000 rows in each files. > > these are the areas i need help: > 1. how can i only read in the files with the string patterns ggg or fff as > part of the file names? > for instance, I only need the file names with the ggg or fff in it > xxxxx_ggg_yyyyy_1.txt > yyyy_fff_yyyy_xxx.txt > > i don't need to read in the files, such as xxxx_aaa_yyyy.txt >Look at the function list.files() and grep(). list.fiels reads all filenames in a directory into R, with grep you can search the file for the pattern "ggg" and "fff" finally, use read.table to load them.> 2.how cam rename the files: > > for instance: xxxxx_ggg_yyyyy_1.txt======>changed to ggg1a.txt >? file.rename There are several functions, starting with file. , to do file operations operation system independent.> > > 3.after the files read in, how can i only keep the rows with the aaa and > bbb, everything elses show be removed from the files, but the files still > remain the same file name? > > for instance, in the xxxxx_ggg_yyyyy_1.txt file, it shouls looks like: > name count > > 1. aaa 100 > 2. bbb 2000 > 3. aaa 300 > 4. bbb 400 > >again, check grep or subset. Cheers, Rainer> > Thanks so lot, I am very new to R, I am looking forward to any helps from > you. > > -- > View this message in context: > http://r.789695.n4.nabble.com/how-to-process-this-in-R-tp2281283p2281283.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- NEW GERMAN FAX NUMBER!!! Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Natural Sciences Building Office Suite 2039 Stellenbosch University Main Campus, Merriman Avenue Stellenbosch South Africa Cell: +27 - (0)83 9479 042 Fax: +27 - (0)86 516 2782 Fax: +49 - (0)321 2125 2244 email: Rainer@krugs.de Skype: RMkrug Google: R.M.Krug@gmail.com [[alternative HTML version deleted]]
I don't have R on the netbook I'm writing this from, but I *think* the following will work:> 1. how can i only read in the files with the string patterns ggg or fff as > part of the file names? > ? for instance, I only need the file names with the ggg or fff in it > ? ? ?xxxxx_ggg_yyyyy_1.txt > ? ? ?yyyy_fff_yyyy_xxx.txt > > ? ? i don't need to read in the files, such as xxxx_aaa_yyyy.txtMaybe something like: d <- "C:\\FilesFolder\\" f <- list.files(path = d, pattern = "(ggg)|(fff)", full.names = TRUE) x <- lapply(f, read.table, header = TRUE)> 2.how cam rename the files: > > ? for instance: xxxxx_ggg_yyyyy_1.txt ======> changed to ggg1a.txtsee ?file.rename> 3.after the files read in, how can i only keep the rows with the aaa and > bbb, everything elses show be removed from the files, but the files still > remain the same file name? > > ? ?for instance, in the xxxxx_ggg_yyyyy_1.txt file, it shouls looks like: > ?name ? ?count > > 1. aaa ? ?100 > 2. bbb ? ?2000 > 3. aaa ? ?300 > 4. bbb ? ?400Something along the following lines should work: DF <- data.frame(name=rep(c("aaa", "bbb"), 2), count = c(100, 2000, 300, 400)) name count 1 aaa 100 2 bbb 2000 3 aaa 300 4 bbb 400 subset(DF, name == "aaa") name count 1 aaa 100 3 aaa 300 HTH Tony Breyal