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 > Sent: Monday, June 27, 2016 10:45 AM > To: David L Carlson <dcarlson at tamu.edu>; Bert Gunter > <bgunter.4567 at gmail.com> > Cc: 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> > > An: "Bert Gunter" <bgunter.4567 at gmail.com> > > Cc: "R Help" <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 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.
G.Maubach at weinwolf.de
2016-Jun-27 11:43 UTC
[R] Antwort: RE: 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> An: "G.Maubach at weinwolf.de" <G.Maubach at weinwolf.de>, Kopie: "r-help at r-project.org" <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 > Sent: Monday, June 27, 2016 10:45 AM > To: David L Carlson <dcarlson at tamu.edu>; Bert Gunter > <bgunter.4567 at gmail.com> > Cc: 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> > > An: "Bert Gunter" <bgunter.4567 at gmail.com> > > Cc: "R Help" <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 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]]
Duncan Murdoch
2016-Jun-27 11:57 UTC
[R] Antwort: RE: Antwort: Fw: Re: Subscripting problem with is.na()
On 27/06/2016 7:43 AM, G.Maubach at 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.pikal at precheza.cz> > An: "G.Maubach at weinwolf.de" <G.Maubach at weinwolf.de>, > Kopie: "r-help at r-project.org" <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 >> Sent: Monday, June 27, 2016 10:45 AM >> To: David L Carlson <dcarlson at tamu.edu>; Bert Gunter >> <bgunter.4567 at gmail.com> >> Cc: 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> >>> An: "Bert Gunter" <bgunter.4567 at gmail.com> >>> Cc: "R Help" <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 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 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. >
G.Maubach at weinwolf.de
2016-Jun-27 12:52 UTC
[R] Antwort: RE: Antwort: Fw: Re: Subscripting problem with is.na()
Hi All, Petr, Bert, David, Ivan, Duncan and Rui helped me to develop a function able to replace NA's in variables IF NEEDED: #------------------------------------------------------------------------------- # Module : t_replace_na.R # Author : Georg Maubach # Date : 2016-06-27 # Update : 2016-06-27 # Description : Replace NA with another value # Source System : R 3.3.0 (64 Bit) # Target System : R 3.3.0 (64 Bit) # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #--------1---------2---------3---------4---------5---------6---------7---------8 t_version = "2016-06-27" t_module_name = "t_replace_na.R" cat( paste0("\n", t_module_name, " (Version: ", t_version, ")", "\n", "\n", "This software comes with ABSOLUTELY NO WARRANTY.", "\n", "\n")) # If do_test is not defined globally define it here locally by un-commenting it t_do_test <- FALSE # [ Function Defintion ]-------------------------------------------------------- t_replace_na <- function(dataset, variables, value) { # Replace NA with another given value # # Args: # dataset (data frame, data table): # Object with dimnames, e.g. data frame, data table. # variables (character vector): # List of variable names. # # Operation: # NA is replaced by the value given with the parameter "value". # # A factor is converted explicitly with as.character(), the missing value # replacement is done and then the character vector is converted back with # as.factor(). Thus NA becomes a category of the new factor variable. # # Caution: # Please check your data in case you replace NA within factors due to # explicit type conversion. Tests were done only for the below given # dataset. # # Returns: # Original dataset. # # Error handling: # None. # # Credits: https://www.mail-archive.com/r-help at r-project.org/msg236537.html for (variable in variables) { 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) } # [ Test Defintion ]------------------------------------------------------------ t_test <- function(do_test = FALSE) { if (do_test == TRUE) { cat("\n", "\n", "Test function t_count_na()", "\n", "\n") # Example dataset ds_example <- data.frame(a=c(1,NA,2), b = rep(NA,3), c = c("A","b",NA)) cat("\n", "\n", "Example dataset before function call", "\n", "\n") cat("Variables and their classes:\n") print(sapply(ds_example, class)) cat("Dataset:\n") print(ds_example) cat("\n", "\n", "Function call", "\n", "\n") ds_result <- t_replace_na(ds_example, "a", value = -1) cat("\n", "\n", "Dataset after function call", "\n", "\n") print(ds_result) cat("\n", "\n", "Function call", "\n", "\n") ds_result <- t_replace_na(ds_example, "b", value = -2) cat("\n", "\n", "Example dataset after function call", "\n", "\n") print(ds_result) cat("\n", "\n", "Function call", "\n", "\n") ds_result <- t_replace_na(ds_example, "c", value = -3) cat("\n", "\n", "Example dataset after function call", "\n", "\n") print(ds_result) } } # [ Test Run ]------------------------------------------------------------------ t_test(do_test = t_do_test) # [ Clean up ]------------------------------------------------------------------ rm("t_module_name", "t_version", "t_do_test", "t_test") # EOF . Please note: R has capabilities to handle NA correctly. There is often no need to recode NA. Also NA might or might not have meaning. You have to decide with regard to the meaning of the original data and the business problem. Kind regards Georg Von: PIKAL Petr <petr.pikal at precheza.cz> An: "G.Maubach at weinwolf.de" <G.Maubach at weinwolf.de>, Kopie: "r-help at r-project.org" <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 > Sent: Monday, June 27, 2016 10:45 AM > To: David L Carlson <dcarlson at tamu.edu>; Bert Gunter > <bgunter.4567 at gmail.com> > Cc: 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> > > An: "Bert Gunter" <bgunter.4567 at gmail.com> > > Cc: "R Help" <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 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.
PIKAL Petr
2016-Jun-27 14:14 UTC
[R] Antwort: RE: Antwort: Fw: Re: Subscripting problem with is.na()
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]]