[R] mystery "158"

Peter Langfelder peter.langfelder at gmail.com
Wed Nov 22 00:00:17 CET 2017


Your data frame fam contains factors. Turn it into character strings using

fam$Family = as.character(fam$Family)

and try again. It may be helpful if you read up on R's factors, see ?factor.

HTH,

Peter

On Tue, Nov 21, 2017 at 2:14 PM, Glen Forister <gforister at gmail.com> wrote:
> This is a simple problem, but a mystery to me.
> I'm trying to grab $Family "Scelionidae" from one dataframe and put it into
> another dataframe occupied with NA in $Family.  The result is a "158" ends
> up there instead of Scelionidae.
> Simply put      fam$Family[1] <- least$Family[1]
>
> If I have made a mistake here, can somebody point it out.  I've included
> the simple steps I got there showing the structure and heads of the objects.
> =====  add a col of NA  = Family
>> least$Family <- NA; str(least)
> 'data.frame':    243 obs. of  6 variables:
>  $ sp    : int  1 3 5 6 8 11 13 15 18 19 ...
>  $ Fallon: int  14 11 109 6 1 44 70 23 4 100 ...
>  $ Dimen : int  10 13 52 2 1 19 18 0 2 116 ...
>  $ Farm  : int  6 2 3 0 0 2 0 1 2 1 ...
>  $ Sums  : int  30 26 164 8 2 65 88 24 8 217 ...
>  $ Family: logi  NA NA NA NA NA NA ...
>>head(least,2)
>   sp Fallon Dimen Farm Sums Family
> 1  1     14    10    6   30     NA
> 3  3     11    13    2   26     NA
>>
>> #next change the property logi to char
>> least$Family <- as.character(least$Family)
>> str(least)
> 'data.frame':    243 obs. of  6 variables:
>  $ sp    : int  1 3 5 6 8 11 13 15 18 19 ...
>  $ Fallon: int  14 11 109 6 1 44 70 23 4 100 ...
>  $ Dimen : int  10 13 52 2 1 19 18 0 2 116 ...
>  $ Farm  : int  6 2 3 0 0 2 0 1 2 1 ...
>  $ Sums  : int  30 26 164 8 2 65 88 24 8 217 ...
>  $ Family: chr  NA NA NA NA ...
>>#  This is where I will grab the info to put into the above.
>> head(fam,2)
>          Family Sp
> 1   Scelionidae  1
> 2         Aphid  2
>>#  This shows the id of my object I want to copy
>> fam$Family[1]
> [1] Scelionidae
> 180 Levels:  ? ? = 97 ? immature ? sp sample ?? ???? 1 2 3 ... wolf?
>>
>># This shows me copying Scelionidae into dataframe least
>> least$Family[1] <- fam$Family[1]
>>
>>#    Here is where I don't get what I expect, but 158
>>str(least);
> 'data.frame':    243 obs. of  6 variables:
>  $ sp    : int  1 3 5 6 8 11 13 15 18 19 ...
>  $ Fallon: int  14 11 109 6 1 44 70 23 4 100 ...
>  $ Dimen : int  10 13 52 2 1 19 18 0 2 116 ...
>  $ Farm  : int  6 2 3 0 0 2 0 1 2 1 ...
>  $ Sums  : int  30 26 164 8 2 65 88 24 8 217 ...
>  $ Family: chr  "158" NA NA NA ...
>>head(least, 1)
>   sp Fallon Dimen Farm Sums Family
> 1  1     14     10        6       30    158
>>
>>#    Showing what I wanted to copy still exists.
>> fam$Family[1]
> [1] Scelionidae
> 180 Levels:  ? ? = 97 ? immature ? sp sample ?? ???? 1 2 3 ... wolf?
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list