Search code examples
rpipelinemutate

Pipe operator wont work in R mutate() function


I am trying to use the pipe operator in dealing with a dataframe in R, but it seems like it does not want to work. I have loaded the tidyverse package, and still does not work. This is the error message:

Error in `mutate()`:
ℹ In argument: `kolokasi`.
Caused by error:
! object 'kolokasi' not found
Run `rlang::last_trace()` to see where the error occurred.

This is the example of dataframe I am dealing with

kolokasi_gay <- data.frame(lex1 = c('геи', "геи", "геи", "геи"),
                           lex2 = c('лесбиянка', 'бисексуал', 'пропаганда', 'активист'),
                           w1w2 = c(256, 30, 45, 50),
                           w1 = c(1000, 1000, 1000, 1000),
                           w2 = c(1000, 214, 33, 1325))

kolokasi_lesbian <- data.frame(lex1 = c('лесбиянка', "лесбиянка", "лесбиянка", "лесбиянка"),
                           lex2 = c('лесбиянка', 'бисексуал', 'пропаганда', 'активист'),
                           w1w2 = c(256, 30, 45, 50),
                           w1 = c(1000, 1000, 1000, 1000),
                           w2 = c(1000, 214, 33, 1325))

This is the code that I am running

kolokasi <- rbind(kolokasi_gay, kolokasi_lesbian) %>%
  select(1:5) %>%
  mutate(kolokasi, logdice = 14 + log2((2*w1w2)/(w1+w2)))

I wonder why I have an error in the mutate() on pipe operator when I am running the whole code? What did I do wrong here?

I can run the code, but doing it separately, not as a pipeline operator.

Your help is appreciated, thanks

I have ran the tidyverse library, and when I ran this code whole code at once

kolokasi_gay <- data.frame(lex1 = c('геи', "геи", "геи", "геи"),
                           lex2 = c('лесбиянка', 'бисексуал', 'пропаганда', 'активист'),
                           w1w2 = c(256, 30, 45, 50),
                           w1 = c(1000, 1000, 1000, 1000),
                           w2 = c(1000, 214, 33, 1325))

kolokasi_lesbian <- data.frame(lex1 = c('лесбиянка', "лесбиянка", "лесбиянка", "лесбиянка"),
                           lex2 = c('лесбиянка', 'бисексуал', 'пропаганда', 'активист'),
                           w1w2 = c(256, 30, 45, 50),
                           w1 = c(1000, 1000, 1000, 1000),
                           w2 = c(1000, 214, 33, 1325))

kolokasi <- rbind(kolokasi_gay, kolokasi_lesbian) %>%
  select(1:5) %>%
  mutate(kolokasi, logdice = 14 + log2((2*w1w2)/(w1+w2)))

It gives me this error message

Error in `mutate()`:
ℹ In argument: `kolokasi`.
Caused by error:
! object 'kolokasi' not found
Run `rlang::last_trace()` to see where the error occurred.

I can deal with this by running the code one by one, but its a hassle because I need to redo the code.


Solution

  • You are piping in the dataframe so the first argument of 'mutate' should be omitted. Try:

    kolokasi <- rbind(kolokasi_gay, kolokasi_lesbian) %>%
      mutate(logdice = 14 + log2((2*w1w2)/(w1+w2)))
    

    I also removed the "select" since you only have 5 columns anyway.