Well, ?factor does not say anything about this behaviour (assigning numeric code
instead of level of factor). And actually if you do assignment for whole vector
the result is different (vector in data frame is changed to factor).
> temp2$fff[1]<-vec[1]
> head(temp2,2)
pokus minuty fff
1 T42 240 3
2 T42 300 <NA>> temp2$fff<-vec
> head(temp2,2)
pokus minuty fff
1 T42 240 c
2 T42 300 c>
> is.factor(vec[1])
[1] TRUE
I am not experieenced enough to explain what is happening but it is probably
combination selection ?"[" and assignment ?"<-"
operation.
I was not able to pinpoint explanation of this in help pages but maybe I only
did not read it correctly.
dput(temp2)
temp2 <- structure(list(pokus = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L
), .Label = c("T42", "T43", "T44",
"T45", "T46", "T47", "T48",
"T49"), class = "factor"), minuty = structure(c(2L, 3L, 4L,
2L,
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L), .Label = c("180", "240", "300",
"360", "420", "480"
), class = "factor"), fff = c(NA_character_, NA_character_,
NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
)), .Names = c("pokus", "minuty", "fff"),
row.names = c(NA, -23L
), class = "data.frame")
> dput(vec)
vec <- structure(c(3L, 3L, 2L, 5L, 3L, 4L, 2L, 2L, 1L, 1L, 5L, 3L, 4L,
1L, 2L, 5L, 2L, 4L, 5L, 5L, 2L, 5L, 4L), .Label = c("a",
"b",
"c", "d", "e"), class = "factor")
Cheers
Petr
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Peter
> Langfelder
> Sent: Wednesday, November 22, 2017 12:00 AM
> To: Glen Forister <gforister at gmail.com>
> Cc: R <r-help at r-project.org>
> Subject: Re: [R] mystery "158"
>
> 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.
>
> ______________________________________________
> 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.
________________________________
Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny
pouze jeho adres?t?m.
Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho
odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho
syst?mu.
Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email
jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat.
Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i
zpo?d?n?m p?enosu e-mailu.
V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?:
- vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a
to z jak?hokoliv d?vodu i bez uveden? d?vodu.
- a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout;
Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce
s dodatkem ?i odchylkou.
- trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m
dosa?en?m shody na v?ech jej?ch n?le?itostech.
- odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost
??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn?
pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn?
osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi
?i osob? j?m zastoupen? zn?m?.
This e-mail and any documents attached to it may be confidential and are
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender.
Delete the contents of this e-mail with all attachments and its copies from your
system.
If you are not the intended recipient of this e-mail, you are not authorized to
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by
modifications of the e-mail or by delay with transfer of the email.
In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately
accept such offer; The sender of this e-mail (offer) excludes any acceptance of
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into
any contracts on behalf of the company except for cases in which he/she is
expressly authorized to do so in writing, and such authorization or power of
attorney is submitted to the recipient or the person represented by the
recipient, or the existence of such authorization is known to the recipient of
the person represented by the recipient.