Home > Software engineering >  R: Converting the Following Code to DPLYR
R: Converting the Following Code to DPLYR

Time:12-12

I am working with the R programming language. I have the following table:

age=18:29
height=c(76.1,77,78.1,78.2,78.8,79.7,79.9,81.1,81.2,81.8,82.8,83.5)
gender=c("M","F","M","M","F","F","M","M","F","M","F","M")
testframe = data.frame(age=age,height=height,height2=height,gender=gender,gender2=gender)

head(testframe)

  age height height2 gender gender2
1  18   76.1    76.1      M       M
2  19   77.0    77.0      F       F
3  20   78.1    78.1      M       M
4  21   78.2    78.2      M       M
5  22   78.8    78.8      F       F
6  23   79.7    79.7      F       F

In the above table, I want to remove columns that have identical entries but have different names. This can be done as follows (in Base R):

no_dup = testframe[!duplicated(as.list(testframe))]

 head(no_dup)
  age height gender
1  18   76.1      M
2  19   77.0      F
3  20   78.1      M
4  21   78.2      M
5  22   78.8      F
6  23   79.7      F

My Question: Does anyone know how to convert the above code testframe[!duplicated(as.list(testframe)) into "DPLYR" commands? Is this possible?

Thanks!

CodePudding user response:

Here is one option using tidyverse. I used purrr::map to convert each column into a list, then I found the lists that were not duplicated. For dplyr::select, you cannot use a logical vector, so we can use which to return only the TRUE columns (i.e., not duplicated). Then, we can use the index value to select the columns.

library(tidyverse)

testframe %>%
  dplyr::select(., which(purrr::map(., c) %>%
                           duplicated(.) %>%
                           `!`))

Output

   age height gender
1   18   76.1      M
2   19   77.0      F
3   20   78.1      M
4   21   78.2      M
5   22   78.8      F
6   23   79.7      F
7   24   79.9      M
8   25   81.1      M
9   26   81.2      F
10  27   81.8      M
11  28   82.8      F
12  29   83.5      M

You could also do it without purrr.

testframe %>%
  dplyr::select(., which(as.list(.) %>%
                           duplicated %>%
                           `!`))

Just for fun, here I only use tidyverse (though much more verbose). It also requires pivoting the dataframe multiple times.

testframe %>%
  tibble::rownames_to_column() %>%
  dplyr::mutate_all(as.character) %>%
  tidyr::pivot_longer(-rowname) %>%
  tidyr::pivot_wider(names_from = rowname, values_from = value) %>%
  dplyr::distinct_at(vars(-name), .keep_all = TRUE) %>%
  tidyr::pivot_longer(-name, names_to = "rowname", values_to = "value") %>%
  tidyr::pivot_wider(names_from = name, values_from = value) %>%
  dplyr::select(-rowname)

Data

testframe <-
  structure(
    list(
      age = 18:29,
      height = c(76.1, 77, 78.1, 78.2,
                 78.8, 79.7, 79.9, 81.1, 81.2, 81.8, 82.8, 83.5),
      height2 = c(76.1,
                  77, 78.1, 78.2, 78.8, 79.7, 79.9, 81.1, 81.2, 81.8, 82.8, 83.5),
      gender = c("M", "F", "M", "M", "F", "F", "M", "M", "F", "M",
                 "F", "M"),
      gender2 = c("M", "F", "M", "M", "F", "F", "M", "M",
                  "F", "M", "F", "M")
    ),
    class = "data.frame",
    row.names = c(NA, -12L)
  )
  • Related