sahimat
sahimat

Reputation: 145

R: Checking variable types

I'm quite new to R, and I am currently trying to write a code with some foolproofing.

For example, I have a function where let's say it takes (n,c,l) where the variables are supposed to be numerical, character, and logical types.

Is there a way so that I can check these things? for example, I've tried is.integer(3) ... this returns FALSE.

Ideally, what I'm looking for is for example, suppose abc() is some function that will test if l==T or l==F (checking if the proper logical is input. then: abc(T) gives TRUE, and abc(2) gives FALSE.

Also is there a way to check if n is specifically an integer? I mean I could check if (n%%1==0), but is there a specific function for this?

Thank you kindly in advance for what may seem to be a very basic question.

Upvotes: 1

Views: 5895

Answers (1)

Kumar Manglam
Kumar Manglam

Reputation: 2832

As suggested in the comments, you can use

is.numeric(3)#check whether numeric
is.integer(3L)#check whether integer
is.logical(TRUE)#Check whether logical
is.logical(2)#will return false
is.character("abc")#check whether character
is.character(4)#will return false

Similarly, you can check for other data types in R. Hope this is of help.

Upvotes: 3

Related Questions