0

I have got many factors with measurements, f.e.:

measurement1Height;
[1] 176 177 180 181 177
Levels: 176 177 180 181

measurement2Height;
[1] 176 177 180 181 177
Levels: 176 177 180 181

measurement3Height;
[1] 176 177 180 181 176
Levels: 176 180 181

measurement1Weight;
[1] 73 79 85 85 80
Levels: 73 79 80 85

measurement2Weight;
[1] 75 80 84 85 80
Levels: 75 80 84 85

measurement3Weight;
[1] 74 79 85 84 76
Levels: 74 76 79 84 85

measurement1Height <- factor(c(176, 177, 180, 181, 177));
measurement2Height <- factor(c(176, 177, 180, 181, 177));
measurement3Height <- factor(c(176, 177, 180, 181, 176));
measurement1Weight <- factor(c(73, 79, 85, 85, 80));
measurement2Weight <- factor(c(75, 80, 84, 85, 80));
measurement3Weight <- factor(c(74, 79, 85, 84, 76));

I need to get output table where all data is grouped by measurement:

        Measurement1    Measurement2    Measurement3    
        Height  Weight  Height  Weight  Height  Weight
Person1 176     73      176     75      176     74
Person2 177     79      177     80      177     79
Person3 180     85      180     84      180     85
Person4 181     85      181     85      181     84
Person5 177     80      177     80      176     76  

Is there a way to do it quickly?

Jens
  • 261
  • 1
  • 3
  • 16
  • 2
    Could you add some data to have a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – s__ Oct 10 '18 at 12:28
  • Which class do you want your output (data.frame, matrix...) ? Or do you want a "table plot" with that headers? Because imho in the first case it's not straightforward. – s__ Oct 10 '18 at 12:44
  • I hoped for data.frame but wait for any solution – Jens Oct 10 '18 at 12:46
  • You're expecting a `data.frame` format with two rows of headers, where the first one expands in two columns?? Maybe you're looking for something like this instead: https://cran.r-project.org/web/packages/tables/vignettes/tables.pdf or this: https://cran.r-project.org/web/packages/expss/vignettes/tables-with-labels.html – AntoniosK Oct 10 '18 at 12:58

1 Answers1

1

Heres's a quick & dirty solution using dplyr.

library(dplyr)

# your data
measurement1Height <- factor(c(176, 177, 180, 181, 177))
measurement2Height <- factor(c(176, 177, 180, 181, 177))
measurement3Height <- factor(c(176, 177, 180, 181, 176))
measurement1Weight <- factor(c(73, 79, 85, 85, 80))
measurement2Weight <- factor(c(75, 80, 84, 85, 80))
measurement3Weight <- factor(c(74, 79, 85, 84, 76))

# operations
data_names <- as.list(sort(paste0(paste0("measurement", 1:3), rep(c("Height", "Weight"), each=3))))
data <- lapply(data_names, get)
names(data) <- data_names
data <- data %>%
  bind_rows() %>%
  mutate(Person = paste("Person", 1:nrow(bind_rows(data)))) %>%
  select(Person, everything())

# here's your new data
data

After all, I'd recommend using integer and numeric values instead of factors (depending on the goal you have).

alex_555
  • 1,092
  • 1
  • 14
  • 27
  • @Jens Can you show me what your output looks like or show me your error messages? Did you load `dplyr`? What exactly doesn't work? Do you want to include the person-numbers in the first column? Then have a look at the adjustment I made above. Please be aware that this solution won't give you two rows of headers. Is this what you're missing? – alex_555 Oct 11 '18 at 06:36