Is there a way to call a variable inside data frame in cases wherein it has same syntax with a built-in function?

For example,

  1. I have a variable (column name) = "mode" in a sample data frame.

image

  1. And there is a built-in function with R with its name "mode"

image

How do I tell my script to get values from the first one (1) and not on second one (2)?

Can you give an example where the column name mode causes a problem? Here are a couple of examples where there is no problem.

DF <- data.frame(mode = 1:4)
DF$mode
[1] 1 2 3 4
library(dplyr)
DF |> mutate(mode2 = mode * 2)
  mode mode2
1    1     2
2    2     4
3    3     6
4    4     8

Here is a sample one and also the error it shows.

When you select() the columns, you do not include class, so when you try to filter() there is no class column and that causes the error. Compare the two version below

library(tidyverse)

#This throws an error
mpg |> select(manufacturer,model, cty, hwy) |> 
  filter(manufacturer == "volkswagen") |> 
  filter(class == "compact")

#No error
mpg |> select(manufacturer,model, cty, hwy, class) |> 
  filter(manufacturer == "volkswagen") |> 
  filter(class == "compact")

Thank you! I understand now.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.