Question Details

No question body available.

Tags

r lapply

Answers (2)

June 26, 2026 Score: 5 Rep: 178,936 Quality: Medium Completeness: 50%

The problem you have is due to non-standard R names, which cannot start with a letter or contain a symbol such as -. Rather than manually adding the backticks and converting from strings to symbols, we can use the base R function as.symbol to convert non-standard names to symbols, then use Rui's reformulate trick.

analytes [[1]] #> 2-ADNT ~ Extracted #> #> [[2]] #> 4-ADNT ~ Extracted #> #> [[3]] #> 26-DANT ~ Extracted
June 26, 2026 Score: 3 Rep: 79,006 Quality: Medium Completeness: 80%

You need backticks around character strings with special symbols such as the minus sign in your analytes. But you don't have to manually edit the strings, create them with sprintf and pipe the result to lapply/reformulate.

analytes  lapply(reformulate, termlabels = "Extracted")
#> [[1]]
#> 2-ADNT ~ Extracted
#> 
#> 
#> [[2]]
#> 4-ADNT ~ Extracted
#> 
#> 
#> [[3]]
#> 26-DANT ~ Extracted
#> 

Created on 2026-06-26 with reprex v2.1.1


You can write a function applying sprintf in case there are special characters in your strings and pipe to reformulate.

make_formula 
    lapply(reformulate, termlabels = termlabels)
}

analytes 2-ADNT ~ Extracted #> #> #> [[2]] #> 4-ADNT ~ Extracted #> #> #> [[3]] #> 26-DANT ~ Extracted #>

Created on 2026-06-26 with reprex v2.1.1