[R] editing a data.frame
john.polo
jpolo at mail.usf.edu
Sat Jun 7 05:22:30 CEST 2008
Daniel Folkinshteyn wrote:
> works for me:
> > sub('1.00', '1', '1.00E-20')
> [1] "1E-20"
when i input what you wrote, i get the same result. but that doesn't
change the value for TreeTag at row 1501, it's just floating around in
space. if i try it for yr1bp$TreeTag[1501], which is 1.00E-20 i get this:
> yr1bp$TreeTag[1501]<-sub("1.00", "1", yr1bp$TreeTag[1501])
Warning message:
In `[<-.factor`(`*tmp*`, 1501, value = "1E-20") :
invalid factor level, NAs generated
and then 1501 turns into:
1501 <NA> 2001 adult 32.5
which is less useful than the way it was originally input. thanks for
the suggestion.
john
> 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.)
thanks for that suggestion. it could come in handy.
> 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.
>>
More information about the R-help
mailing list