[R] mystery "158"

William Dunlap wdunlap at tibco.com
Wed Nov 22 17:07:56 CET 2017


I think the '[<-' function for atomic types doesn't pay attention
to the class of the right hand side, only to its mode.  It strips all
the attributes of the RHS, including the class.

> f <- function(x) { x[2] <- factor("z", levels=letters) ; x }
> f(101:103)
[1] 101  26 103
> f(c("One","Two","Three"))
[1] "One"   "26"    "Three"
> f(exp(1i*(1:4)))
[1]  0.5403023+0.8414710i 26.0000000+0.0000000i -0.9899925+0.1411200i
-0.6536436-0.7568025i
> f(list(One=1,Two=2))
$One
[1] 1

$Two
[1] 26


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Nov 22, 2017 at 1:15 AM, PIKAL Petr <petr.pikal at precheza.cz> wrote:

> 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.
> ______________________________________________
> 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list