-4

I am trying to make a table in R for a particular set of data by creating five columns (name, total number of a particular name, mean, SD, and range).

I already have a dataset(sizes) with columns: name, height(H), and weight(W) and I would like to create a table using the sizes data with columns labeled as: name, total number of a particular name, mean of H, SD of H, and range of H, mean of W, SD of W, and range of W. However, I am having trouble extracting the data by name. Any suggestions?

example of dataframe (sizes) desired table

This may be a rookie question, but that is exactly what I am in the R world so any help would be great!

zachO
  • 3
  • 2
  • 3
    Please try to post an example of your data and expected output, see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and https://stackoverflow.com/help/how-to-ask – RLave Aug 24 '18 at 11:11
  • Please edit **the question** with the output of `dput(sizes)` or, if it is too big, with the output of `dput(head(sizes, 20))`. – Rui Barradas Aug 24 '18 at 11:17

2 Answers2

1

I think that example below will be helpful:

library(dplyr)
data<-iris
data %>% group_by(Species) %>% summarise(Count= n(),Mean=mean(data$Sepal.Width),SD=sd(data$Sepal.Width))

Where you will be grouping by column with Names

0

The following does what you want. I have used the built in dataset iris, selecting one name column and two numeric columns.

The main function is aggregate. You should go through its help page. At an R command prompt run ?aggregate.

sizes <- iris[5:3]
names(sizes) <- c("name", "height", "weight")
head(sizes)

stats <- function(x){
    c(Sum = sum(x), Mean = mean(x), SD = sd(x), Range = range(x))
}

agg <- aggregate(. ~ name, data = sizes, stats)

colnames(agg$height) <- paste("height", colnames(agg$height), sep = ".")
colnames(agg$height) <- sub("Range1", "Min", colnames(agg$height))
colnames(agg$height) <- sub("Range2", "Max", colnames(agg$height))

colnames(agg$weight) <- paste("weight", colnames(agg$weight), sep = ".")
colnames(agg$weight) <- sub("Range1", "Min", colnames(agg$weight))
colnames(agg$weight) <- sub("Range2", "Max", colnames(agg$weight))

agg <- cbind(agg[1], agg$height, agg$weight)

agg
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66