Consider the following dataset:? I need to replace NAs with "-" but I lose my numeric formatting??fall.estimate <- structure(list(`Salmon` = c("salmon River",?"Ant Creek", "big Creek", "oso River", "linda Creek"), `baseline` = c(80874.384012, 361.1997, 5012.8311,?638.6912, 402.104433333333), `target` = c(160000,?720, 10000, 450, 800), `1992` = c(27618.4365, 0, 3587.61719,?NA, NA), `1993` = c(100027.82328, NA, 5647.83116, NA, NA), `1994` = c(99414.57438,?NA, 12896.93753, NA, NA), `1995` = c(235027.00518, NA, 32059.63037,?NA, NA), `1996` = c(143004.6423, NA, 17191.2152, NA, NA), `1997` = c(112796.88894,?NA, 27365.24435, NA, NA), `1998` = c(102858.8148, NA, 20539.17372,?NA, NA), `1999` = c(94113.26562, NA, 21916.44213, NA, NA)), row.names = c(NA,?-5L), class = c("tbl_df", "tbl", "data.frame")) fall.estimatestr(fall.estimate)#convert to class dataframefall.estimate <- as.data.frame(fall.estimate) #Remove all decimalsfall.estimate[,-1] <-round(fall.estimate[,-1],0)#Replace NA's' with dash '-'fall.estimate[is.na(fall.estimate)] <- "-" #Here all my columns get converted to character#Try to convert back to numericfall.estimate <- mutate_all(fall.estimate, function(x) as.numeric(as.character(x)))?fall.estimate#But I get these warnings aand my dashes dissapearQuestion: How can I replace my NAs with dashes and keep all my dataframecolumns as numeric??Warning messages:1: In FUN(newX[, i], ...) : NAs introduced by coercion2: In FUN(newX[, i], ...) : NAs introduced by coercion3: In FUN(newX[, i], ...) : NAs introduced by coercion4: In FUN(newX[, i], ...) : NAs introduced by coercion5: In FUN(newX[, i], ...) : NAs introduced by coercion6: In FUN(newX[, i], ...) : NAs introduced by coercion7: In FUN(newX[, i], ...) : NAs introduced by coercion8: In FUN(newX[, i], ...) : NAs introduced by coercion9: In FUN(newX[, i], ...) : NAs introduced by coercion Thanks beforehand [[alternative HTML version deleted]]
Data frames are NOT spreadsheets. Don't treat them like spreadsheets. All elements in a column are parts of a vector which means they all have the same data type. On the other hand, if you want to generate formatted output in HTML, LaTeX, or Word, there are many tools for generating formatted tables in the data output phase of data analysis, and it is common to convert everything to character format intentionally then. On October 19, 2019 12:44:26 AM PDT, Felipe Carrillo via R-help <r-help at r-project.org> wrote:>Consider the following dataset:? I need to replace NAs with "-" but I >lose my numeric formatting??fall.estimate <- structure(list(`Salmon` >c("salmon River",?"Ant Creek", "big Creek", "oso River", "linda >Creek"), `baseline` = c(80874.384012, 361.1997, 5012.8311,?638.6912, >402.104433333333), `target` = c(160000,?720, 10000, 450, 800), `1992` >c(27618.4365, 0, 3587.61719,?NA, NA), `1993` = c(100027.82328, NA, >5647.83116, NA, NA), `1994` = c(99414.57438,?NA, 12896.93753, NA, NA), >`1995` = c(235027.00518, NA, 32059.63037,?NA, NA), `1996` >c(143004.6423, NA, 17191.2152, NA, NA), `1997` = c(112796.88894,?NA, >27365.24435, NA, NA), `1998` = c(102858.8148, NA, 20539.17372,?NA, NA), >`1999` = c(94113.26562, NA, 21916.44213, NA, NA)), row.names >c(NA,?-5L), class = c("tbl_df", "tbl", "data.frame")) >fall.estimatestr(fall.estimate)#convert to class dataframefall.estimate ><- as.data.frame(fall.estimate) >#Remove all decimalsfall.estimate[,-1] ><-round(fall.estimate[,-1],0)#Replace NA's' with dash >'-'fall.estimate[is.na(fall.estimate)] <- "-" >#Here all my columns get converted to character#Try to convert back to >numericfall.estimate <- mutate_all(fall.estimate, function(x) >as.numeric(as.character(x)))?fall.estimate#But I get these warnings >aand my dashes dissapearQuestion: How can I replace my NAs with dashes >and keep all my dataframecolumns as numeric??Warning messages:1: In >FUN(newX[, i], ...) : NAs introduced by coercion2: In FUN(newX[, i], >...) : NAs introduced by coercion3: In FUN(newX[, i], ...) : NAs >introduced by coercion4: In FUN(newX[, i], ...) : NAs introduced by >coercion5: In FUN(newX[, i], ...) : NAs introduced by coercion6: In >FUN(newX[, i], ...) : NAs introduced by coercion7: In FUN(newX[, i], >...) : NAs introduced by coercion8: In FUN(newX[, i], ...) : NAs >introduced by coercion9: In FUN(newX[, i], ...) : NAs introduced by >coercion >Thanks beforehand > > > [[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.-- Sent from my phone. Please excuse my brevity.
You are correct. I didnt explain well and failed to mention that this is for knitr::kable. I already figured it out.? Sent from Yahoo Mail on Android On Sat, Oct 19, 2019 at 1:04 AM, Jeff Newmiller<jdnewmil at dcn.davis.ca.us> wrote: Data frames are NOT spreadsheets. Don't treat them like spreadsheets. All elements in a column are parts of a vector which means they all have the same data type. On the other hand, if you want to generate formatted output in HTML, LaTeX, or Word, there are many tools for generating formatted tables in the data output phase of data analysis, and it is common to convert everything to character format intentionally then. On October 19, 2019 12:44:26 AM PDT, Felipe Carrillo via R-help <r-help at r-project.org> wrote:>Consider the following dataset:? I need to replace NAs with "-" but I >lose my numeric formatting??fall.estimate <- structure(list(`Salmon` >c("salmon River",?"Ant Creek", "big Creek", "oso River", "linda >Creek"), `baseline` = c(80874.384012, 361.1997, 5012.8311,?638.6912, >402.104433333333), `target` = c(160000,?720, 10000, 450, 800), `1992` >c(27618.4365, 0, 3587.61719,?NA, NA), `1993` = c(100027.82328, NA, >5647.83116, NA, NA), `1994` = c(99414.57438,?NA, 12896.93753, NA, NA), >`1995` = c(235027.00518, NA, 32059.63037,?NA, NA), `1996` >c(143004.6423, NA, 17191.2152, NA, NA), `1997` = c(112796.88894,?NA, >27365.24435, NA, NA), `1998` = c(102858.8148, NA, 20539.17372,?NA, NA), >`1999` = c(94113.26562, NA, 21916.44213, NA, NA)), row.names >c(NA,?-5L), class = c("tbl_df", "tbl", "data.frame")) >fall.estimatestr(fall.estimate)#convert to class dataframefall.estimate ><- as.data.frame(fall.estimate) >#Remove all decimalsfall.estimate[,-1] ><-round(fall.estimate[,-1],0)#Replace NA's' with dash >'-'fall.estimate[is.na(fall.estimate)] <- "-" >#Here all my columns get converted to character#Try to convert back to >numericfall.estimate <- mutate_all(fall.estimate, function(x) >as.numeric(as.character(x)))?fall.estimate#But I get these warnings >aand my dashes dissapearQuestion: How can I replace my NAs with dashes >and keep all my dataframecolumns as numeric??Warning messages:1: In >FUN(newX[, i], ...) : NAs introduced by coercion2: In FUN(newX[, i], >...) : NAs introduced by coercion3: In FUN(newX[, i], ...) : NAs >introduced by coercion4: In FUN(newX[, i], ...) : NAs introduced by >coercion5: In FUN(newX[, i], ...) : NAs introduced by coercion6: In >FUN(newX[, i], ...) : NAs introduced by coercion7: In FUN(newX[, i], >...) : NAs introduced by coercion8: In FUN(newX[, i], ...) : NAs >introduced by coercion9: In FUN(newX[, i], ...) : NAs introduced by >coercion >Thanks beforehand > > >??? [[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.-- Sent from my phone. Please excuse my brevity. [[alternative HTML version deleted]]