as.numeric error for a list vs dataframe

Hi everyone,

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)

check$a_ampicillin_mic<- ifelse(grepl(">",check$a_ampicillin_mic),
as.numeric(substring(check$a_ampicillin_mic,2))+1,
check$a_ampicillin_mic)

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.

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.