R wont recognize numeric columns as numeric

Hi all,
I recently had a problem after importing data from excel. In the process I gave command to take all the columns as numeric. But when I try to summarise and find mean it tell that doubles are there and an error

bmi %>% summarize(mean(HT) ,median(HT),mean(WT))

A tibble: 1 x 3

mean(HT) median(HT) mean(WT)

1 NA NA NA

class(bmi$HT)
[1] "numeric"

please help me guys. I cannot use r as of this

Hi and welcome!

Do you maybe have NAs in your data? That would explain why you get no results for mean and median. If that is the case, you can just add na.rm=TRUE to your mean and median functions to get a proper result.


bmi %>% summarize(mean(HT, na.rm = TRUE), median(HT, na.rm = TRUE),mean(WT,na.rm = TRUE))

If that does not solve your problem, it would be good if you could post a reproducible example so that everyone can understand and look into your problem.

2 Likes

Please post the output of

dput(head(bmi))

On the lines just before and after the pasted output, type three back ticks like this:
```
You pasted output here
```

1 Like

structure(list(PID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 12, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100), SEX = c("F", "M", "F",
"F", "M", "F", "M", "M", "F", "F", "M", "M", "F", "F", "F", "F",
"F", "M", "M", "M", "M", "M", "M", "F", "F", "F", "M", "F", "M",
"F", "F", "M", "M", "F", "F", "M", "M", "M", "M", "F", "M", "M",
"M", "F", "F", "F", "F", "F", "F", "M", "F", "F", "F", "F", "F",
"M", "F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F", "M",
"M", "M", "F", "M", "M", "F", "M", "M", "F", "M", "F", "M", "F",
"F", "F", "F", "F", "F", "M", "M", "M", "F", "F", "F", "M", "M",
"M", "M", "F", "M", "M", "M", "F", "M"), HT = c(164.7, 168.9,
160.3, 162.9, 172.3, 155.2, 169.2, 166.6, 155.1, 162.1, 181.3,
168.3, 160.1, 157.4, 157.5, 167.9, 160.4, 174.3, 169.2, 172.5,
166.3, 166.6, 161, 165, 155.8, 168.5, 163.5, 157.1, 170.3, 163,
157, 173, 169.4, 166.2, 161.2, 164.8, 174.1, 162.3, 167, 154.9,
177.8, 178.8, 168.3, 150.6, 164.8, 161.5, 163.4, 162.1, 153.3,
181.3, 161.9, 160.6, 152.9, 163.2, 160.6, 169.7, 153.1, 169.6,
162.9, 166.1, 166.1, 166.7, 170.4, 170.3, 164.9, 166.9, 158.2,
177.2, NA, 173.9, 158.4, 170.6, 179.3, 165.6, 168.8, 168.2, 158.3,
173, 159.6, 174.2, 166.4, 158.9, 161.5, 171.3, 155.8, 154.8,
170.1, 164.1, 167, 159, 156.6, 159.2, 171.2, 169.6, 165.7, 178.8,
152.4, 174.4, 156.4, 173.1, 163.5, 166.1), WT = c(61.3, 65.7,
57.6, 62.9, 58.3, 55.2, 70.2, 60.6, 60.3, 58.9, 71, 62.5, 53.3,
53.1, 61.7, 68.1, 54.5, 73.6, 66.4, 67, 62.2, 63.1, 56.6, 63.7,
59.1, 61.4, NA, 48.2, 59.9, 63, 52.3, 70.3, 63.4, 55.5, 59.9,
62.3, 71, 57.3, 69.5, 53.7, 70.7, 74.5, 62.5, 50.8, 63, 57.2,
59.3, 60.8, 55.2, 69, 58.4, NA, 58.4, 63.2, 61, 68.4, 51.4, 69.5,
66, 62.3, 62.3, 60, 62.2, 65.2, 61, 65.7, 56, 69.3, 61.5, 66.4,
51.3, 69.1, 70, 62.5, 65.9, 67.5, 54.1, 70.8, 58.9, 69.9, 66.7,
52.1, 56.5, 64.9, 51.5, 53.2, 64.6, 60.2, 61.7, 58, 58.7, 61.1,
66, 67.2, 62.3, 76.6, 45.6, 72.3, 55.5, 69.1, 60.4, 63.8)), row.names = c(NA,
-102L), class = c("tbl_df", "tbl", "data.frame"))

Using your data, I got the following result.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
bmi <- structure(list(PID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                              13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
                              29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 12, 43,
                              44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
                              59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
                              75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
                              91, 92, 93, 94, 95, 96, 97, 98, 99, 100), SEX = c("F", "M", "F",
                                                                                "F", "M", "F", "M", "M", "F", "F", "M", "M", "F", "F", "F", "F",
                                                                                "F", "M", "M", "M", "M", "M", "M", "F", "F", "F", "M", "F", "M",
                                                                                "F", "F", "M", "M", "F", "F", "M", "M", "M", "M", "F", "M", "M",
                                                                                "M", "F", "F", "F", "F", "F", "F", "M", "F", "F", "F", "F", "F",
                                                                                "M", "F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F", "M",
                                                                                "M", "M", "F", "M", "M", "F", "M", "M", "F", "M", "F", "M", "F",
                                                                                "F", "F", "F", "F", "F", "M", "M", "M", "F", "F", "F", "M", "M",
                                                                                "M", "M", "F", "M", "M", "M", "F", "M"), HT = c(164.7, 168.9,
                                                                                                                                160.3, 162.9, 172.3, 155.2, 169.2, 166.6, 155.1, 162.1, 181.3,
                                                                                                                                168.3, 160.1, 157.4, 157.5, 167.9, 160.4, 174.3, 169.2, 172.5,
                                                                                                                                166.3, 166.6, 161, 165, 155.8, 168.5, 163.5, 157.1, 170.3, 163,
                                                                                                                                157, 173, 169.4, 166.2, 161.2, 164.8, 174.1, 162.3, 167, 154.9,
                                                                                                                                177.8, 178.8, 168.3, 150.6, 164.8, 161.5, 163.4, 162.1, 153.3,
                                                                                                                                181.3, 161.9, 160.6, 152.9, 163.2, 160.6, 169.7, 153.1, 169.6,
                                                                                                                                162.9, 166.1, 166.1, 166.7, 170.4, 170.3, 164.9, 166.9, 158.2,
                                                                                                                                177.2, NA, 173.9, 158.4, 170.6, 179.3, 165.6, 168.8, 168.2, 158.3,
                                                                                                                                173, 159.6, 174.2, 166.4, 158.9, 161.5, 171.3, 155.8, 154.8,
                                                                                                                                170.1, 164.1, 167, 159, 156.6, 159.2, 171.2, 169.6, 165.7, 178.8,
                                                                                                                                152.4, 174.4, 156.4, 173.1, 163.5, 166.1), WT = c(61.3, 65.7,
                                                                                                                                                                                  57.6, 62.9, 58.3, 55.2, 70.2, 60.6, 60.3, 58.9, 71, 62.5, 53.3,
                                                                                                                                                                                  53.1, 61.7, 68.1, 54.5, 73.6, 66.4, 67, 62.2, 63.1, 56.6, 63.7,
                                                                                                                                                                                  59.1, 61.4, NA, 48.2, 59.9, 63, 52.3, 70.3, 63.4, 55.5, 59.9,
                                                                                                                                                                                  62.3, 71, 57.3, 69.5, 53.7, 70.7, 74.5, 62.5, 50.8, 63, 57.2,
                                                                                                                                                                                  59.3, 60.8, 55.2, 69, 58.4, NA, 58.4, 63.2, 61, 68.4, 51.4, 69.5,
                                                                                                                                                                                  66, 62.3, 62.3, 60, 62.2, 65.2, 61, 65.7, 56, 69.3, 61.5, 66.4,
                                                                                                                                                                                  51.3, 69.1, 70, 62.5, 65.9, 67.5, 54.1, 70.8, 58.9, 69.9, 66.7,
                                                                                                                                                                                  52.1, 56.5, 64.9, 51.5, 53.2, 64.6, 60.2, 61.7, 58, 58.7, 61.1,
                                                                                                                                                                                  66, 67.2, 62.3, 76.6, 45.6, 72.3, 55.5, 69.1, 60.4, 63.8)), row.names = c(NA,
                                                                                                                                                                                                                                                            -102L), class = c("tbl_df", "tbl", "data.frame"))

bmi %>% summarize(MeanHT=mean(HT, na.rm = TRUE), MedHt=median(HT, na.rm = TRUE),MeanWt=mean(WT,na.rm = TRUE))
#> # A tibble: 1 x 3
#>   MeanHT MedHt MeanWt
#>    <dbl> <dbl>  <dbl>
#> 1   165.  166.   61.9

Created on 2020-12-04 by the reprex package (v0.3.0)

Thanks guys.Silly me. I have not applied na.rm seperately for each variable.Thnks a lot. I was discouraged. But you guys like my family helped me

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Hi,
Thanks a lot for your reply. Nope. I have tried na.rm the same results comesd. Here is my data set. Can anyone try doing it?
How can I share my excel data set here? Can you again please tell me