steve
steve

Reputation: 79

using the %>% operator from dplyr in R

I've seen examples of code using the %>% operator from the dplyr (or tidyverse) package in R to perform a sequence of actions on the same object or data frame. However, I have never been able to get this to work for my own code. For example, in the code below I try to substitute the "1:2=" portion of each cell in a column and then convert the column to numeric. This works fine if I do each step one at a time, but results in an error when I try and pipe one command to the next.

Can anyone help me understand what am I doing wrong here?

> df <- as.data.frame(vroom("manhattan_practice_data.txt", col_names = c("chromosome", "position", "num_SNPs", "prop_SNPs_coverage", "min_coverage", "AvsDD", "AvsWD", "DDvsWD")))
Rows: 79 Columns: 8                                                                                                                                           
-- Column specification ---------------------------------------------------------------------------------------------------
Delimiter: " "
chr (4): chromosome, AvsDD, AvsWD, DDvsWD
dbl (4): position, num_SNPs, prop_SNPs_coverage, min_coverage

i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(df)
'data.frame':   79 obs. of  8 variables:
 $ chromosome        : chr  "A01" "A01" "A01" "A01" ...
 $ position          : num  139 149 384 544 547 552 558 615 686 693 ...
 $ num_SNPs          : num  1 1 1 1 1 1 1 1 1 1 ...
 $ prop_SNPs_coverage: num  1 1 1 1 1 1 1 1 1 1 ...
 $ min_coverage      : num  104 32 79 46 48 52 60 30 98 94 ...
 $ AvsDD             : chr  "1:2=0.00000000" "1:2=0.08624012" "1:2=0.13233606" "1:2=0.00000000" ...
 $ AvsWD             : chr  "1:3=0.10843987" "1:3=0.00000000" "1:3=0.12724615" "1:3=0.23923465" ...
 $ DDvsWD            : chr  "2:3=0.33696506" "2:3=0.38416539" "2:3=0.00000000" "2:3=0.26549660" ...
 - attr(*, "spec")=
  .. cols(
  ..   chromosome = col_character(),
  ..   position = col_double(),
  ..   num_SNPs = col_double(),
  ..   prop_SNPs_coverage = col_double(),
  ..   min_coverage = col_double(),
  ..   AvsDD = col_character(),
  ..   AvsWD = col_character(),
  ..   DDvsWD = col_character()
  .. )
 - attr(*, "problems")=<externalptr> 

> df <- df %>% gsub("1:2=","",as.character(AvsDD)) %>% as.numeric(AvsDD)
Error in gsub("1:2=", "", as.character(AvsDD)) : object 'AvsDD' not found

However, this works fine when I do each step one at a time, and results in the AvsDD column being converted to numeric:

> df$AvsDD <- gsub("1:2=","",as.character(df$AvsDD))
> df$AvsDD <- as.numeric(df$AvsDD)
> str(df)
'data.frame':   79 obs. of  8 variables:
 $ chromosome        : chr  "A01" "A01" "A01" "A01" ...
 $ position          : num  139 149 384 544 547 552 558 615 686 693 ...
 $ num_SNPs          : num  1 1 1 1 1 1 1 1 1 1 ...
 $ prop_SNPs_coverage: num  1 1 1 1 1 1 1 1 1 1 ...
 $ min_coverage      : num  104 32 79 46 48 52 60 30 98 94 ...
 $ AvsDD             : num  0 0.0862 0.1323 0 0 ...
 $ AvsWD             : chr  "1:3=0.10843987" "1:3=0.00000000" "1:3=0.12724615" "1:3=0.23923465" ...
 $ DDvsWD            : chr  "2:3=0.33696506" "2:3=0.38416539" "2:3=0.00000000" "2:3=0.26549660" ...
 - attr(*, "spec")=
  .. cols(
  ..   chromosome = col_character(),
  ..   position = col_double(),
  ..   num_SNPs = col_double(),
  ..   prop_SNPs_coverage = col_double(),
  ..   min_coverage = col_double(),
  ..   AvsDD = col_character(),
  ..   AvsWD = col_character(),
  ..   DDvsWD = col_character()
  .. )
 - attr(*, "problems")=<externalptr>

Upvotes: 1

Views: 215

Answers (1)

akrun
akrun

Reputation: 887028

With dplyr, the transformation operations are done within mutate

library(dplyr)
df <- df %>% 
    mutate(AvsDD = as.numeric(gsub("1:2=","",as.character(AvsDD), fixed = TRUE))) 

Although, it is possible to do the kind of operations OP did by extracting the column (.$) and wrapping with {} but it is not a nice approach


As there are multiple columns, we may use across

library(stringr)
df <- df %>%
     mutate(across(c(AvsDD, AvsWD, DDvsWD), ~ as.numeric(str_remove(., ".*\\="))))

Upvotes: 5

Related Questions