On 27/06/2016 7:43 AM, g.maub...@weinwolf.de wrote:
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?

I don't think you'll find an explicit list of rules in the R documentation. It does imply the rules, however, by saying that a data frame is a list which can be indexed as a matrix.

So if you want to treat your dataset as a list of columns, use single component list indexing: dataset[columnname] to give another list, dataset[[columnname]] to extract the column as a vector.

If you want to treat it as a matrix of values, use two indices:

dataset[row, column]

to extract the entry (or entries, if row or column contains more than one value).

Duncan Murdoch


Kind regards

Georg





Von:    PIKAL Petr <petr.pi...@precheza.cz>
An:     "g.maub...@weinwolf.de" <g.maub...@weinwolf.de>,
Kopie:  "r-help@r-project.org" <r-help@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-boun...@r-project.org] On Behalf Of
g.maub...@weinwolf.de
Sent: Monday, June 27, 2016 10:45 AM
To: David L Carlson <dcarl...@tamu.edu>; Bert Gunter
<bgunter.4...@gmail.com>
Cc: r-help@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" <dcarl...@tamu.edu>
An: "Bert Gunter" <bgunter.4...@gmail.com>
Cc: "R Help" <r-help@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@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.


        [[alternative HTML version deleted]]



______________________________________________
R-help@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@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.

Reply via email to