Dynamic Format Matching Strings in R

I want to check if there is no column for the header of the input data frame. The problem occurs when I use fread(filename)

to change the missing header to the default name "V + Number of column" (ex: V1 for the first column, V2, etc.). I've read the documentation for this feature and can't seem to change this default for headers. So with that in mind, I want all the containing columns to V + number

be missing from the headers. It will be very helpful if someone explains how to do something like this:

if(string == (V1, V2, ... VN)){ do something }

Similar to comparison if String is 'V' plus an integer.

+3


source to share


2 answers


you can get all Vx column values ​​using str_extract from the stringr package. I think.

library(stringr)
str_extract(colnames(myDataFrame), "V\\d*")

      

Example:



dt <- data.frame("aaaa" = c(1:5), "V2" = c(6:10), "V3123" = c(11:15), "V455" = c(16:20))
str_extract(colnames(dt), "V\\d*")

      

"NA" "V2" "V3123" "V455"

Then you can do what you need with the column names

+1


source


Like this? I don't know exactly what you want to do with the missing column names; if you want to replace them with spaces or write their positions, adjust the print statements to make a vector with, for example, everything i

where It missing

.



x <- data.frame(A=1,B=2,V3=3,V4=4,C=5)    
z <- names(x)

for(i in 1:length(z)){
  if(z[i]==paste0("V", i)){
    print("It missing")
  } else {
    print("Not missing")
  }
}

      

+1


source







All Articles