Analysis in R: Detecting strings as numbers. The “type.convert” command

RAnalytics
スポンサーリンク

When manipulating data, the data may change from numbers to letters. A shortcut to the solution is to occasionally check the data structure with the str command and convert it with the as.integer command.

Another option is to use the “type.convert” command, which recognizes character data as numbers. The type.convert command is characterized by the “na.string” option, which specifies a string of missing values. You can use this command if as.integer returns an error.

Checked with R version 4.2.2.

スポンサーリンク

Example

See the command and package help for details.

#Save numbers as strings
ChaTest <- c("1", "2", "3")
#Checked Class
class(ChaTest)
[1] "character"
#Calculation, of course error
ChaTest[1] + ChaTest[2]
ChaTest[1] + ChaTest[2] error
#Calculation, of course error
sum(ChaTest)
sum(ChaTest) error

#Use the "type.convert" command
#Detect strings as numbers
class(type.convert(ChaTest))
[1] "integer"
#Calculation,Get Correct Answer
type.convert(ChaTest[1]) + type.convert(ChaTest[2])
[1] 3
#Calculation!!
sum(type.convert(ChaTest))
[1] 6

I hope this makes your analysis a little easier !!

Amazon audibleの登録の紹介

プライム会員限定で2024年7月22日まで3か月無料体験キャンペーン開催中です。無料体験後は月額1,500円で聞き放題です。なお、聞き放題対象外の本はAudible会員であれば非会員価格の30%引きで購入することが可能です。

Amazon audibleはプロのナレーターが朗読した本をアプリで聞くことができるサービスで、オフライン再生も可能です。通勤や作業のお供にAmazon audibleのご登録はいかがでしょうか。

・AmazonのAudible

https://amzn.to/3L4FI5o

Copied title and URL