Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a data frame of numerics,integers and string. I would like to check which columns are integers and I do

raw<-read.csv('./rawcorpus.csv',head=F)
ints<-sapply(raw,is.integer)

anyway this gives me all false. So I have to make a little change

nums<-sapply(raw,is.numeric)
ints2<-sapply(raw[,nums],function(col){return(!(sum(col%%1)==0))})

The second case works fine. My question is: what is actually checking the 'is.integer' function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
311 views
Welcome To Ask or Share your Answers For Others

1 Answer

By default, R will store all numbers as double precision floating points, i.e., the numeric. Three useful functions class, typeof and storage.mode will tell you how a value is stored. Try:

x <- 1
class(x)
typeof(x)
storage.mode(x)

If you want x to be integer 1, you should do with suffix "L"

x <- 1L
class(x)
typeof(x)
storage.mode(x)

Or, you can cast numeric to integers by:

x <- as.integer(1)
class(x)
typeof(x)
storage.mode(x)

The is.integer function checks whether the storage mode is integer or not. Compare

is.integer(1)
is.integer(1L)

You should be aware that some functions actually return numeric, even if you expect it to return integer. These include round, floor, ceiling, and mod operator %%.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...