Home > OS >  Why is R showing equal values for different entries rather than counting actual frequencies (excludi
Why is R showing equal values for different entries rather than counting actual frequencies (excludi

Time:08-17

I have been learning R recently. While executing the following codes, I found the following results:

Code:

data<-read.csv("pen.csv",header=TRUE)
head(data)
require(reshape2)
dat<-melt(data=data)
names(dat)<-c("species","weight")
summary(dat)

Result:

> summary(dat)

|      species     |  weight         |  
|------------------|-----------------|  
| Adelie   :6      | Min.   :181.0   |
| Gentoo   :6      | 1st Qu.:193.0   |
| Chinstrap:6      | Median :202.0   |
|                  | Mean   :202.3   |  
|                  | 3rd Qu.:215.0   | 
|                  | Max.   :216.0   |
|                  | NA's   :3       |

I was expecting the result like this:

|      species     |  weight         |  
|------------------|-----------------|  
| Adelie   :5      | Min.   :181.0   |
| Gentoo   :6      | 1st Qu.:193.0   |
|Chinstrap :4      | Median :202.0   |
|                  | Mean   :202.3   |  
|                  | 3rd Qu.:215.0   | 
|                  | Max.   :216.0   |
|                  | NA's   :3       |

I even tried considering species as a factor (dat$species<-as.factor(dat$species)).Got the same result. Could anyone help? (Here, I have attached the data set)

Adelie Gentoo Chinstrap
181 215 202
186 215 193
195 215 210
NA 216 198
193 215 NA
190 210 NA

CodePudding user response:

You just have some NA values in the original table, so the counts are a bit off. melt does not default to removing them, unless you set na.rm = TRUE:

read_table("Adelie  Gentoo  Chinstrap
181 215 202
186 215 193
195 215 210
NA  216 198
193 215 NA
190 210 NA") %>%
  melt(na.rm = TRUE) %>%
  setNames(c("species","weight")) %>%
  summary()

 #      species      weight     
 # Adelie   :5   Min.   :181.0  
 # Gentoo   :6   1st Qu.:193.0  
 # Chinstrap:4   Median :202.0  
 #               Mean   :202.3  
 #               3rd Qu.:215.0  
 #               Max.   :216.0  
  •  Tags:  
  • r
  • Related