[R] Antwort: RE: Antwort: Fw: Re: Subscripting problem with is.na()

PIKAL Petr petr.pikal at precheza.cz
Mon Jun 27 16:14:29 CEST 2016


Hi

On top of what Duncan wrote you can check results yourself

> str(iris[,"Sepal.Length"])
num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...

here you get vector and data.frame class is lost. The result is same as
> str(iris$Sepal.Length)
num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...

However

> str(iris["Sepal.Length"])
'data.frame':   150 obs. of  1 variable:
$ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...

here the data frame class is preserved.

I subscript rectangular objects (arrays, matricex, data, frames) exclusively by

object[ , , something, ] or in case of data frame object.df[sub, ]

and lists with

object.l[sub] or object.l[[sub]]

so I never had problems with it.

Cheers
Petr


From: G.Maubach at weinwolf.de [mailto:G.Maubach at weinwolf.de]
Sent: Monday, June 27, 2016 1:43 PM
To: PIKAL Petr <petr.pikal at precheza.cz>
Cc: r-help at r-project.org
Subject: Antwort: RE: [R] Antwort: Fw: Re: Subscripting problem with is.na()

Hi Petr,

many thanks for your reply and the examples.

My subscripting problems drive me nuts.

I have understood that dataset[variable] is semantically identical to dataset[, variable] cause dataset[variable] takes all cases because no other subscripts are given.

Where can I lookup the rules when to use the comma and when not?

Kind regards

Georg





Von:        PIKAL Petr <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>>
An:        "G.Maubach at weinwolf.de<mailto:G.Maubach at weinwolf.de>" <G.Maubach at weinwolf.de<mailto:G.Maubach at weinwolf.de>>,
Kopie:        "r-help at r-project.org<mailto:r-help at r-project.org>" <r-help at r-project.org<mailto:r-help at r-project.org>>
Datum:        27.06.2016 11:03
Betreff:        RE: [R] Antwort: Fw: Re:  Subscripting problem with is.na()
________________________________



Hi

see in line

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of
> G.Maubach at weinwolf.de<mailto:G.Maubach at weinwolf.de>
> Sent: Monday, June 27, 2016 10:45 AM
> To: David L Carlson <dcarlson at tamu.edu<mailto:dcarlson at tamu.edu>>; Bert Gunter
> <bgunter.4567 at gmail.com<mailto:bgunter.4567 at gmail.com>>
> Cc: r-help at r-project.org<mailto:r-help at r-project.org>
> Subject: [R] Antwort: Fw: Re: Subscripting problem with is.na()
>
> Hi David,
> Hi Bert,
>
> many thanks for the valuable discussion on NA in R (please see extract
> below). I follow your arguments leaving NA as they are for most of the
> time. In special occasions however I want to replace the NA with another
> value. To preserve the newly acquired knowledge for me I wrote this
> function:
>
> -- cut --
> t_replace_na <- function(dataset, variable, value) {
>  if(inherits(dataset[[variable]], "factor") == TRUE) {
>    dataset[variable] <- as.character(dataset[variable])
>    print(class(dataset[variable]))
>    dataset[, variable][is.na(dataset[, variable])] <- value
>    dataset[variable] <- as.factor(dataset[variable])
>    print(class(dataset[variable]))
>  } else {
>    dataset[, variable][is.na(dataset[, variable])] <- value
>  }
>  return(dataset)
> }
>

<snip>

> class(ds_test[, "c"])
> test_class(ds_test, "c")
> warning("'c' should be factor NOT data.frame.
> In addition data.frame != factor")
> -- cut --
>
> Why do I get different results for the same function if it is inside or
> outside my own function definition?

Because you still are missing the way how to subscript data frames.

test_class <- function(dataset, variable) {
 if(inherits(dataset[, variable], "factor") == TRUE) {
   return(c(class(dataset[,variable]), TRUE))
####                                 ^^^^
} else {
   return(c(class(dataset[,variable]), FALSE))
######                            ^^^^
 }
}

> test_class(ds_test, "a")
[1] "numeric" "FALSE"
> test_class(ds_test, "c")
[1] "factor" "TRUE"
>

If you properly arrange commas in your function you get desired result

p_replace_na <- function(dataset, variable, value) {
if(inherits(dataset[,variable], "factor") == TRUE) {
  dataset[,variable] <- as.character(dataset[,variable])
  print(class(dataset[,variable]))
  dataset[, variable][is.na(dataset[, variable])] <- value
  dataset[, variable] <- as.factor(dataset[, variable])
  print(class(dataset[, variable]))
} else {
  dataset[, variable][is.na(dataset[, variable])] <- value
}
return(dataset)
}

> p_replace_na(ds_test, "c", value = -3)
[1] "character"
[1] "factor"
  a  b  c
1  1 NA  A
2 NA NA  b
3  2 NA -3

> t_replace_na(ds_test, "c", value = -3)
[1] "data.frame"
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
>

Cheers
Petr



>
> Kind regards
>
> Georg
>
> --------------------------------
>
> > Gesendet: Donnerstag, 23. Juni 2016 um 21:14 Uhr
> > Von: "David L Carlson" <dcarlson at tamu.edu<mailto:dcarlson at tamu.edu>>
> > An: "Bert Gunter" <bgunter.4567 at gmail.com<mailto:bgunter.4567 at gmail.com>>
> > Cc: "R Help" <r-help at r-project.org<mailto:r-help at r-project.org>>
> > Betreff: Re: [R] Subscripting problem with is.na()
> >
> > Good point. I did not think about factors. Also your example raises
> another issue since column c is logical, but gets silently converted to
> numeric. This would seem to get the job done assuming the conversion is
> intended for numeric columns only:
> >
> > > test <- data.frame(a=c(1,NA,2), b = c("A","b",NA), c= rep(NA,3))
> > > sapply(test, class)
> >         a         b         c
> > "numeric"  "factor" "logical"
> > > num <- sapply(test, is.numeric)
> > > test[, num][is.na(test[, num])] <- 0
> > > test
> >   a    b  c
> > 1 1    A NA
> > 2 0    b NA
> > 3 2 <NA> NA
> >
> > David C
>
> ______________________________________________
> R-help at r-project.org<mailto: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-<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.

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

	[[alternative HTML version deleted]]



More information about the R-help mailing list