Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

How is.integer() works in R

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

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

1 Reply

0 votes
by (71.8m points)

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 %%.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...