[R] editing a data.frame

Daniel Folkinshteyn
Fri, 06 Jun 2008 21:31:53 -0000

works for me:
 > sub('1.00', '1', '1.00E-20')
[1] "1E-20"

remember, according to ?sub, it's sub(pattern, repl, string)

try doing it step by step. first, see what yr1bp$TreeTag[1501] is.
then, if it's the right data item, see what the output of
sub("1.00",
"1", yr1bp$TreeTag[1501]) is.
that'll let you figure out where the problem lies.

finally, if all your target strings are of the form 1.00E-20, you could
sub the whole thing with a more general regexp:

sub("([0-9])(\.[0-9]{2})(.*)", "\\1\\3", yourvector)

(it matches a digit, followed by a dot and two digits, followed by
"anything else", and takes out the "dot and two digits" bit
in the
replacement, in the whole vector.)

on 06/06/2008 03:25 PM john.polo said the following:
> dear R users,
>
> the data frame (read in from a csv) looks like this:
>        TreeTag     Census    Stage     DBH
> 1     CW-W740   2001 juvenile     5.8
> 2     CW-W739   2001 juvenile     4.3
> 3     CW-W738   2001 juvenile     4.7
> 4     CW-W737   2001 juvenile     5.4
> 5     CW-W736   2001 juvenile     7.4
> 6     CW-W735   2001 juvenile     5.4
> ...
> 1501 1.00E-20   2001  adult      32.5
>
> i would like to change values under the TreeTag column. as the last
> value shows, some of the tags have decimals followed by 2 decimal
> places. i just want whole numbers, i.e. not 1.00E-20, but 1E-20. i have
> a rough understanding of regexp and grepped all the positions that have
> the inappropriate tags. i tried sub() a couple of different ways, like
> yr1bp$TreeTag[1501]<-sub("1.00", "1",
yr1bp$TreeTag[1501])
> and after turning yr1bp$TreeTag[1501] into <NA>,
> yr1bp$TreeTag[1501]<-sub("", "1E-20",
yr1pb$TreeTag[1501])
> and
> sub("", "1E-20", yr1bp$TreeTag[1501])
> but it's not working. i guess it has something to do with the
data.frame
> characteristics i'm not aware of or don't understand. would i
somehow
> have to tear apart the columns, edit them, and then put it back
> together? not that i know how to do that, but i'm wondering out loud.
>
> john
>
> ______________________________________________
> 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.
>