[R] automatic file input

Ista Zahn izahn at psych.rochester.edu
Mon Aug 22 19:34:13 CEST 2011


On Mon, Aug 22, 2011 at 1:25 PM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:
> Because i have to use the value of i in for loop also.example-
>
>  for(i in 1:100){
>
>  df=read.table($i.out,fill=T,colClasses = "character")
> if(i<=50){
>  df$V6 <- sapply(df$V6, function(a)
>  paste(as.integer(charToRaw(a)), collapse = ' '))}
> else{
>  df$V5 <- sapply(df$V6, function(a)
>  paste(as.integer(charToRaw(a)), collapse = ' '))}
> }
>  so what I was thinking that if i will use paste, then it is working
>
>
>  for(i in 1:100){
>
>  df=read.table(paste(i),fill=T,colClasses = "character")
> if(i<=50){
>  df$V6 <- sapply(df$V6, function(a)
>  paste(as.integer(charToRaw(a)), collapse = ' '))}
> else{
>  df$V5 <- sapply(df$V6, function(a)
>  paste(as.integer(charToRaw(a)), collapse = ' '))}
> }
>
> This works perfect for me.But I have to change my file names from 1.out to 1 and 2.out to 2 etc.because when i used -
>
>  df=read.table(paste(i.out),fill=T,colClasses = "character")
> then it is not working.

use paste(i, ".out", sep = "")

Best,
Ista
>
>
> Thanking you,
> Warm Regards
> Vikas Bansal
> Msc Bioinformatics
> Kings College London
> ________________________________________
> From: istazahn at gmail.com [istazahn at gmail.com] On Behalf Of Ista Zahn [izahn at psych.rochester.edu]
> Sent: Monday, August 22, 2011 6:14 PM
> To: Bansal, Vikas
> Cc: r-help at r-project.org
> Subject: Re: [R] automatic file input
>
> Why don't you just use list.files() and iterate over the result in
> your for loop?
>
> fileNames <- list.files(pattern = "\\.out")
> myFiles <- list()
> for(i in fileNames) {
> myFiles[[i]] <- read.table(i, fill=T,colClasses = "character")
> }
>
> ?
>
> Best,
> Ista
>
>
> On Mon, Aug 22, 2011 at 12:34 PM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:
>> Dear Ista
>>
>> I have searched about the problem and came to know that we can make a list of our file names.But the thing is I am using this code-
>>
>>
>> define<- function()
>>  { repeat{
>>  hojy=readline("Enter the name of your file: ")
>>  if(file.exists(hojy)==T)
>> {return(hojy)
>> break}
>> else
>> print("File does not exist.Please enter again")}
>>  }
>>
>>
>> for(i in 1:100){
>>
>> df=read.table(define(),fill=T,colClasses = "character")
>> df$V6 <- sapply(df$V6, function(a)
>>  paste(as.integer(charToRaw(a)), collapse = ' '))}
>>
>> so here i am using a function which is asking user to input the file name.But I was thinking that if I will delete this function and will use the read.table like this-
>>
>> for(i in 1:100){
>>
>> df=read.table($i.out,fill=T,colClasses = "character")
>> df$V6 <- sapply(df$V6, function(a)
>>  paste(as.integer(charToRaw(a)), collapse = ' '))}
>>
>> if you will check this line-
>> df=read.table($i.out,fill=T,colClasses = "character")
>>
>> I have used $i.out here and every time in for loop i will be 1 then 2 then 3 till 100.So it will become very easy.But this $ sign is not working in the R.Can I do this thing in R.
>>
>>
>>
>>
>> Thanking you,
>> Warm Regards
>> Vikas Bansal
>> Msc Bioinformatics
>> Kings College London
>> ________________________________________
>> From: istazahn at gmail.com [istazahn at gmail.com] On Behalf Of Ista Zahn [izahn at psych.rochester.edu]
>> Sent: Monday, August 22, 2011 5:16 PM
>> To: Bansal, Vikas
>> Cc: r-help at r-project.org
>> Subject: Re: [R] automatic file input
>>
>> Hi Vikas,
>> please do make an effort to search for the answer before posting. A
>> google search for "R read multiple files" will give you everything you
>> need.
>>
>> Best,
>> Ista
>>
>> On Mon, Aug 22, 2011 at 12:08 PM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:
>>> Dear all,
>>>
>>> I have 100 files which are used as input.and I have to input the name of  my files again and again.the name of the files are 1.out, 2.out......100.out.
>>> I want to know if there is anything like perl so that i can use something like this-
>>>
>>>
>>> for($f = 1; $f <= 100; $f++) {
>>>  $file = $f.".out";
>>>
>>>
>>> I have tried this thing in R but it does not work.Can somebody please help me.
>>>
>>>
>>>
>>> Thanking you,
>>> Warm Regards
>>> Vikas Bansal
>>> Msc Bioinformatics
>>> Kings College London
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> --
>> Ista Zahn
>> Graduate student
>> University of Rochester
>> Department of Clinical and Social Psychology
>> http://yourpsyche.org
>>
>
>
>
> --
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list