-1

I have data of the form:

              Department            LengthAfter
1                     A                  8.42
2                     B                 10.93
3                     D                  9.98
4                     A                 10.13
5                     B                 10.54
6                     C                  7.82
7                     A                  9.55
8                     D                 12.53
9                     C                  7.87

I would like to make a new table or dataframe in which the column header is each department (A, B, C, D) and the Lengths under each column are the values on LengthAfter corresponding to each department. e.g.

A       B         C         D
8.42   10.93     7.82      9.98

Can anyone help with this? Thank you

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Welcome to SO; question has nothing to do with `statistics`, kindly do not spam irrelevant tags (removed). – desertnaut Mar 29 '21 at 01:26
  • People are generally more likely to help if you include your data in a reproducible format (i.e., with `dput`). You have multiple readings for each of the departments - how should they be collapsed into a single reading? Is it an average? Or will the departments be repeated multiple times in the columns? – Conor Neilson Mar 29 '21 at 08:04

3 Answers3

1

Using tidyverse, you can use pivot_wider to pivot your data into the desired form. Before that, you will need to sort (arrange) by Department first, if you want to include the values from LengthAfter in the order of appearance, and have the columns in order as above.

library(tidyverse)

df %>%
  arrange(Department) %>%
  group_by(Department) %>%
  mutate(rn = row_number()) %>%
  pivot_wider(names_from = "Department", values_from = "LengthAfter") %>%
  select(-rn)

Output

      A     B     C     D
  <dbl> <dbl> <dbl> <dbl>
1  8.42  10.9  7.82  9.98
2 10.1   10.5  7.87 12.5 
3  9.55  NA   NA    NA  
Ben
  • 28,684
  • 5
  • 23
  • 45
0

You can use package reshape2 for this

Library (reshape2)

Df_new <- dcast(df_old, Department~LengthAfter)

Sg2403
  • 1
  • 2
0

Base-R

dept_length <- read.csv("/Users/usr/SO_Department_LengthAfter.tsv", sep="\t");
dl_list <- with(dept_length, tapply(LengthAfter, Department, `c`));
n.obs <- sapply(dl_list, length);
seq.max <- seq_len(max(n.obs));
sapply(dl_list, `[`, i = seq.max);

Returns:

         A     B    C     D
[1,]  8.42 10.93 7.82  9.98
[2,] 10.13 10.54 7.87 12.53
[3,]  9.55    NA   NA    NA

References:

  1. https://stat.ethz.ch/R-manual/R-devel/library/base/html/split.html
  2. How to convert a list consisting of vector of different lengths to a usable data frame in R?
  3. https://eeob-biodata.github.io/R-Data-Skills/05-split-apply-combine/
jubilatious1
  • 1,999
  • 10
  • 18