I'm trying to write an ifelse code to convert certain numbers based on a substring:
as.numeric(substring(check$a_ampicillin_mic,2))+1 <- this seems to work
but
as.numeric(substring(astresult[micvec],2))+1 <- this doesn't work and gives me the error "Error: Must subset columns with a valid subscript vector.
x Subscript micvec has the wrong type `tbl_df<
a_amocla_mic : character ... etc"?
Does anybody know why? I want to use the second method because I'm trying to convert several columns in a dataframe.
The codes I used are as follows (the 1st one works, 2nd one gives me the error)
micvec <- select(astresult, contains("_mic"))
astresult[micvec] <-
for (i in astresult[micvec]){
ifelse(grepl(">",astresult[micvec]),
as.numeric(substring(astresult[micvec],2))+1,
astresult[micvec])
}
Hello.
Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.
Share some representative data that will enable your code to run and show the problematic behaviour.
You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.