Re: [R] editing a data.frame

From: jim holtman <jholtman_at_gmail.com>
Date: Sat, 07 Jun 2008 08:25:56 -0400

Actually change the TreeTag to characters first because you are trying to store in a new factor value that is not there

 yr1bp$TreeTag <- as.character(yr1bp$TreeTag) yr1bp$TreeTag[1501]<-sub("1.00", "1", yr1bp$TreeTag[1501]) # change back to a factor if desired
yr1bp$TreeTag <- factor(yr1bp$TreeTag)

On Sat, Jun 7, 2008 at 8:22 AM, jim holtman <jholtman_at_gmail.com> wrote:

> try:
>
> yr1bp$TreeTag[1501]<-sub("1.00", "1", as.character(yr1bp$TreeTag[1501]))
>
> Since it appears that TreeTag is a factor. This can be verified with
> 'str'.
>
>
> On Fri, Jun 6, 2008 at 11:22 PM, john.polo <jpolo_at_mail.usf.edu> wrote:
>
>> 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<http://www.r-project.org/posting-guide.html>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>> ______________________________________________
>> 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<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
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.
Received on Sat 07 Jun 2008 - 12:32:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 09 Jun 2008 - 12:30:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive