Preserving numeric columns

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Preserving numeric columns

R help mailing list-2
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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Preserving numeric columns

Jeff Newmiller
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 <[hidden email]> 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]]
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Preserving numeric columns

R help mailing list-2
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<[hidden email]> 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 <[hidden email]> 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]]
>
>______________________________________________
>[hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Preserving numeric columns

Jeff Newmiller
Then the polite next step is for you to indicate what that solution was so people searching the archives can learn from your question. Was it to set the kable option?

options(knitr.kable.NA = '-') 

On October 19, 2019 12:50:20 PM PDT, Felipe Carrillo <[hidden email]> wrote:

>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<[hidden email]> 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
><[hidden email]> 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]]
>>
>>______________________________________________
>>[hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Preserving numeric columns

R help mailing list-2
Yes, options(knitr.kable.NA = '-')  is The answer for kable.
Do you happen to know what are the arguments used for gridExtra grid.draw to acomplish the same thing?

Sent from Yahoo Mail on Android
 
  On Sat, Oct 19, 2019 at 1:01 PM, Jeff Newmiller<[hidden email]> wrote:   Then the polite next step is for you to indicate what that solution was so people searching the archives can learn from your question. Was it to set the kable option?

options(knitr.kable.NA = '-') 

On October 19, 2019 12:50:20 PM PDT, Felipe Carrillo <[hidden email]> wrote:

>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<[hidden email]> 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
><[hidden email]> 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]]
>>
>>______________________________________________
>>[hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Preserving numeric columns

Jeff Newmiller
Well, the direct answer is "no", but then again I did not know the answer to the other question until I Googled it either.

When I do the same for grid.draw, it appears to be a generic function for drawing graphical objects... data frames are not grobs, so you must be doing something to convert it first.

I don't use grid graphics to produce tables... I use knitr::kable or the xtable or tables packages within Rmd or Rnw files.

On October 19, 2019 4:41:06 PM PDT, Felipe Carrillo <[hidden email]> wrote:

>Yes, options(knitr.kable.NA = '-')  is The answer for kable.
>Do you happen to know what are the arguments used for gridExtra
>grid.draw to acomplish the same thing?
>
>Sent from Yahoo Mail on Android
>
>On Sat, Oct 19, 2019 at 1:01 PM, Jeff
>Newmiller<[hidden email]> wrote:   Then the polite next step
>is for you to indicate what that solution was so people searching the
>archives can learn from your question. Was it to set the kable option?
>
>options(knitr.kable.NA = '-') 
>
>On October 19, 2019 12:50:20 PM PDT, Felipe Carrillo
><[hidden email]> wrote:
>>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<[hidden email]> 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
>><[hidden email]> 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]]
>>>
>>>______________________________________________
>>>[hidden email] 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.

______________________________________________
[hidden email] 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.