Changing a value in a particular row and column within a text file

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Changing a value in a particular row and column within a text file

Mauricio Zambrano-Bigiarini-2
Dear list,

I need to change a value within a particular line of a plain text file
with characters and numbers, and I haven't found any way of doing this
by using R.

What I have a is a file that doesn't have tabular data (so, I think
that 'read.table' or 'read.delim' are not the right tools for this),
but some text, and I now exactly the row that has to be modified and
the columns within that row that have to be changed with a new
numerical value.

I don't' know any way of reading a text file line by line in R, and
this is the only solution in R that I have figured out so far...

Any idea about how can I do this ?.


Thanks in advance for any help,

Mauricio

--
===============================
Linux user #454569 -- Ubuntu user #17469

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Changing a value in a particular row and column within a text file

T.V. Nguyen
On 17 Dec 2010, at 01:13, Mauricio Zambrano wrote:

> Dear list,
>
> I need to change a value within a particular line of a plain text file
> with characters and numbers, and I haven't found any way of doing this
> by using R.
>
> What I have a is a file that doesn't have tabular data (so, I think
> that 'read.table' or 'read.delim' are not the right tools for this),
> but some text, and I now exactly the row that has to be modified and
> the columns within that row that have to be changed with a new
> numerical value.
>
> I don't' know any way of reading a text file line by line in R, and
> this is the only solution in R that I have figured out so far...
>
> Any idea about how can I do this ?.

readLines will read lines from a flat file and create a character array:

> list <- readLines("twelvedaysofchristmas.txt")
> head(list)
[1] "12 Drummers Drumming" "11 Pipers Piping"    
[3] "10 Lords-a-Leaping"   "9 Ladies Dancing"    
[5] "8 Maids-a-Milking"    "7 Swans-a-Swimming"

You can either edit the line you want manually, or use sub to make changes:

> sub("Lords-a-Leaping","Jaffa Cakes",list)
> head(list)
[1] "12 Drummers Drumming" "11 Pipers Piping"    
[3] "10 Jaffa Cakes"       "9 Ladies Dancing"    
[5] "8 Maids-a-Milking"    "7 Swans-a-Swimming"  

writeLines will write it back:

> writeLines(list,"twelvedaysofchristmas.txt")

Hope this helps, --t

>
>
> Thanks in advance for any help,
>
> Mauricio
>
> --
> ===============================
> Linux user #454569 -- Ubuntu user #17469
>
> ______________________________________________
> [hidden email] 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.

--
Thomson Van Nguyen
Centre for Mathematical Studies
University of Cambridge
Wilberforce Road
Cambridge CB3 0WA, UK

Tel: +44 7597120422
E-mail: [hidden email]
www.ocf.berkeley.edu/~thomson

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Changing a value in a particular row and column within a text file

Duncan Murdoch-2
In reply to this post by Mauricio Zambrano-Bigiarini-2
Mauricio Zambrano wrote:

> Dear list,
>
> I need to change a value within a particular line of a plain text file
> with characters and numbers, and I haven't found any way of doing this
> by using R.
>
> What I have a is a file that doesn't have tabular data (so, I think
> that 'read.table' or 'read.delim' are not the right tools for this),
> but some text, and I now exactly the row that has to be modified and
> the columns within that row that have to be changed with a new
> numerical value.
>
> I don't' know any way of reading a text file line by line in R, and
> this is the only solution in R that I have figured out so far...
>
> Any idea about how can I do this ?.
>

Use readLines to read it, modify the line, use writeLines to write it
out.

Duncan Murdoch

>
> Thanks in advance for any help,
>
> Mauricio
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Changing a value in a particular row and column within a text file

Jim Lemon
In reply to this post by Mauricio Zambrano-Bigiarini-2
On 12/17/2010 08:13 PM, Mauricio Zambrano wrote:

> Dear list,
>
> I need to change a value within a particular line of a plain text file
> with characters and numbers, and I haven't found any way of doing this
> by using R.
>
> What I have a is a file that doesn't have tabular data (so, I think
> that 'read.table' or 'read.delim' are not the right tools for this),
> but some text, and I now exactly the row that has to be modified and
> the columns within that row that have to be changed with a new
> numerical value.
>
> I don't' know any way of reading a text file line by line in R, and
> this is the only solution in R that I have figured out so far...
>
> Any idea about how can I do this ?.

Hi Mauricio,
Have a look at the readLines function. That will get the text into R,
you can then use "sub" or something to change the offending line and
then write the file out again.

Jim

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Changing a value in a particular row and column within a text file

Petr Savicky
In reply to this post by T.V. Nguyen
On Fri, Dec 17, 2010 at 01:49:23AM -0800, T.V. Nguyen wrote:
> On 17 Dec 2010, at 01:13, Mauricio Zambrano wrote:
[...]
> > but some text, and I now exactly the row that has to be modified and
> > the columns within that row that have to be changed with a new
> > numerical value.
[...]
> You can either edit the line you want manually, or use sub to make changes:
>
> > sub("Lords-a-Leaping","Jaffa Cakes",list)
> > head(list)
> [1] "12 Drummers Drumming" "11 Pipers Piping"    
> [3] "10 Jaffa Cakes"       "9 Ladies Dancing"    
> [5] "8 Maids-a-Milking"    "7 Swans-a-Swimming"  

Additionally, if you know the exact character positions, which have
to be changed, then substr() can be used.

  x <- "123456789"
  substr(x, 5, 7) <- "abc"
  x # [1] "1234abc89"

For an exact replacement, the length of the new text should be
the same as of the original field.

Petr Savicky.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Changing a value in a particular row and column within a text file

Mauricio Zambrano-Bigiarini-2
Thank you very much for all your help.

Following your advice, I solved my problem by using the following:

##########################################

lines    <- readLines("myFile.txt")
myline <- lines[myrow]
L         <- nchar(myline)

substr(myline, Col.Ini, Col.Fin) <- NewValue
lines[myrow]                               <- myline
readLines(lines, "myFile.txt")

############################################

The 'readLines' was the only R command that I was missing from other
programming languages, but now I can implement in R the full code in
which I was working. :)

Thanks for your fast and good answer, That's one of the thinks that I
like the most of the R community !.

Kind regards,

Mauricio

--
===============================
Linux user #454569 -- Ubuntu user #17469
===============================


2010/12/17 Petr Savicky <[hidden email]>:

> On Fri, Dec 17, 2010 at 01:49:23AM -0800, T.V. Nguyen wrote:
>> On 17 Dec 2010, at 01:13, Mauricio Zambrano wrote:
> [...]
>> > but some text, and I now exactly the row that has to be modified and
>> > the columns within that row that have to be changed with a new
>> > numerical value.
> [...]
>> You can either edit the line you want manually, or use sub to make changes:
>>
>> > sub("Lords-a-Leaping","Jaffa Cakes",list)
>> > head(list)
>> [1] "12 Drummers Drumming" "11 Pipers Piping"
>> [3] "10 Jaffa Cakes"       "9 Ladies Dancing"
>> [5] "8 Maids-a-Milking"    "7 Swans-a-Swimming"
>
> Additionally, if you know the exact character positions, which have
> to be changed, then substr() can be used.
>
>  x <- "123456789"
>  substr(x, 5, 7) <- "abc"
>  x # [1] "1234abc89"
>
> For an exact replacement, the length of the new text should be
> the same as of the original field.
>
> Petr Savicky.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.