0

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
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
saif
  • 3
  • 2
  • Welcome to Stack Overflow. We cannot read data into R from images. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including the data in a plain text format - for example the output from `dput(data)`. – neilfws Aug 17 '22 at 00:59

1 Answers1

1

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  
dcsuka
  • 2,922
  • 3
  • 6
  • 27
  • Of course! Perhaps you can mark this answer as correct if it has helped you, so others can know that it works. – dcsuka Aug 17 '22 at 03:25