Writing text files out of a dataset

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing text files out of a dataset

lucameyer
Hello,

I am trying to run the following syntax for all cases within the dataframe
"data"

d1 <- data[1,c("material")]
fileConn<-file("TESTI/d1.txt")
writeLines(d1, fileConn)
close(fileConn)

I am trying to use the for function:

 for (i in 1:nrow(data)){
  d[i] <- data[i,c("material")]
  fileConn<-file("TESTI/d[i].txt")
  writeLines(d[i], fileConn)
  close(fileConn)
}

but I get the error:

Object "d" not found

Any suggestion on how I can solve the above?

Thanks,

Luca

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Mr. Luca Meyer
www.lucameyer.com
Reply | Threaded
Open this post in threaded view
|

Re: Writing text files out of a dataset

Eric Berger
You have an error with the filename in the loop.
Try replacing the relevant line wtih
fileConn<-file(sprintf("TESTI/%d.txt",i))

HTH,
Eric


On Fri, Dec 29, 2017 at 4:31 PM, Luca Meyer <[hidden email]> wrote:

> Hello,
>
> I am trying to run the following syntax for all cases within the dataframe
> "data"
>
> d1 <- data[1,c("material")]
> fileConn<-file("TESTI/d1.txt")
> writeLines(d1, fileConn)
> close(fileConn)
>
> I am trying to use the for function:
>
>  for (i in 1:nrow(data)){
>   d[i] <- data[i,c("material")]
>   fileConn<-file("TESTI/d[i].txt")
>   writeLines(d[i], fileConn)
>   close(fileConn)
> }
>
> but I get the error:
>
> Object "d" not found
>
> Any suggestion on how I can solve the above?
>
> Thanks,
>
> Luca
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Writing text files out of a dataset

Rui Barradas
In reply to this post by lucameyer
Hello,

You have to create the vector 'd' outside the loop before using it.

d <- numeric(nrow(data))

Only then comes the loop.

Hope this helps,

Rui Barradas

On 12/29/2017 2:31 PM, Luca Meyer wrote:

> Hello,
>
> I am trying to run the following syntax for all cases within the dataframe
> "data"
>
> d1 <- data[1,c("material")]
> fileConn<-file("TESTI/d1.txt")
> writeLines(d1, fileConn)
> close(fileConn)
>
> I am trying to use the for function:
>
>   for (i in 1:nrow(data)){
>    d[i] <- data[i,c("material")]
>    fileConn<-file("TESTI/d[i].txt")
>    writeLines(d[i], fileConn)
>    close(fileConn)
> }
>
> but I get the error:
>
> Object "d" not found
>
> Any suggestion on how I can solve the above?
>
> Thanks,
>
> Luca
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Writing text files out of a dataset

Jeff Newmiller
This presumes that 'data[["material"]]' is numeric.

It is unnecessary to put the output data into a separate vector d (it is already in a vector that is part of the 'data' data frame).  I would just overwrite `d` (or `d1`) each time through the loop:

 for (i in seq_along( data[["material"]] ) ){
    d <- data[i,c("material")]
    fileConn <- file(paste0("TESTI/d", i, ".txt"))
    writeLines(d, fileConn)
    close(fileConn)
}

or

for ( i in seq_along( data[["material"]] ) ){
   writeLines( data[i,c("material")], file=paste0("TESTI/d", i, ".txt" ) )
}
--
Sent from my phone. Please excuse my brevity.

On December 29, 2017 9:49:20 AM PST, Rui Barradas <[hidden email]> wrote:

>Hello,
>
>You have to create the vector 'd' outside the loop before using it.
>
>d <- numeric(nrow(data))
>
>Only then comes the loop.
>
>Hope this helps,
>
>Rui Barradas
>
>On 12/29/2017 2:31 PM, Luca Meyer wrote:
>> Hello,
>>
>> I am trying to run the following syntax for all cases within the
>dataframe
>> "data"
>>
>> d1 <- data[1,c("material")]
>> fileConn<-file("TESTI/d1.txt")
>> writeLines(d1, fileConn)
>> close(fileConn)
>>
>> I am trying to use the for function:
>>
>>   for (i in 1:nrow(data)){
>>    d[i] <- data[i,c("material")]
>>    fileConn<-file("TESTI/d[i].txt")
>>    writeLines(d[i], fileConn)
>>    close(fileConn)
>> }
>>
>> but I get the error:
>>
>> Object "d" not found
>>
>> Any suggestion on how I can solve the above?
>>
>> Thanks,
>>
>> Luca
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.