Gäng Tian
Gäng Tian

Reputation: 1608

How to search for a specific column name in data

So this is a bit strange, I am pretty new to R and facing this weird problem. I have a data frame, and there is a column called SNDATE which is a combined value of two different columns.

I want to check if the data frame has a column named SN, if it doesn't I will split SNDATE to fill the SN column.

Here is the code

if(!('SN' %in% colnames(data))){
     #do some spliting here
}

Funny thing is, it keeps saying it's there, and the stuff in it never gets triggered. And when I do this:

print(data$SN)

It will print the value of data$SNDATE. So does R have some sort of lazy name filling or something? This is very strange to me.

Thank you very much for the help

Upvotes: 0

Views: 3086

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

When you do

print(data$SN)

it works because $ is using partial name matching. For another example, try

mtcars$m

There is no column named m, so $ partially matches mpg. Unfortunately, this is not used in %in%, so you will need to use the complete exact column name in

if(!('SNDATE' %in% colnames(data))){
     #do some spliting here
}

You could insead use something along the lines of pmatch()

names(mtcars)[2] <- "SNDATE"
names(mtcars)[pmatch("SN", names(mtcars))]
# [1] "SNDATE"

So the if() statement might go something like this -

nm <- colnames(data)
if(!nm[pmatch("SN", nm)] %in% nm) {
    ...
}

Or even

if(is.na(pmatch("SN", names(data)))

might be better

Upvotes: 2

Related Questions