Home > Software engineering >  r: Error: Must extract column with a single valid subscript
r: Error: Must extract column with a single valid subscript

Time:10-08

I have a column in the dataframe with the values: "Q1", Q2", "Q3", etc. I'm trying to separate the letter "Q" from the numeric values using:

DF %>% separate(DF$tmp, c(NA, "Number"), "(?<=[A-Z])(?=[0-9])" ). 

I get the following error:

"Error: Must extract column with a single valid subscript. x Subscript var has size 452 but must be size 1. "

I don't get this error if I create the dataframe manually using:

df <- data.frame(x = c(NA, "Q1", "Q2", "Q10"))

df %>% separate(x, c(NA, "B"), "(?<=[A-Z])(?=[0-9])" )

But I do get the error when working with the existing dataframe (I checked, and is.dataframe returns TRUE)

CodePudding user response:

The command should be

DF %>% 
    separate(tmp, c("Other", "Number"), "(?<=[A-Z])(?=[0-9])" )
  • Related