I'm working with a large dataset of SNPs and some of them are formatted in a way that makes them difficult to work with a machine learning package I'm using. Specifically, the hyphen in some of the column names is causing problems so I'm trying to go through and replace them with an underscore. I know there are probably multiple ways of doing this but this is the one I was trying with my limited knowledge of R, using lapply and str_replace:
library(tidyverse)
exampleData = tribble(
~SNP-1_1, ~SNP-1_2, ~SNP-2_2, ~SNP-2_3,
0, 1, 1, 0,
1, 0, 1, 0,
0, 0, 0, 1
)
colnames = names(exampleData)
colnames = lapply(colnames, str_replace("-", "_"))
This, however, causes the error: Error in fix_replacement(replacement) : argument "replacement" is missing, with no default. My understanding of lapply is that it should take colnames and apply each of the elements individually to str_replace, which should in turn mean that all 3 necessary arguments of str_replace should be there. Does someone know what's going on here? Or if you have a better way of editing the column names I'd be happy to listen to that too.