LOFTER for ipad —— 让兴趣,更有趣

点击下载 关闭
7 Coercion Taming your data
子苔 2019-03-14

Exercise

It is possible to transform your data from one type to the other. Next to the class()      class 的RDocumentation)class function, you can use the as.*()    functions to enforce data to change types.

For example, 

var <- "3"var_num <- as.numeric(var)

converts the character string "3" in var to a numeric 3 and assigns it to var_num. However, keep in my that it is not always possible to convert the types without losing information or getting errors.但是转换会损失一些信息 譬如小数点的数字没法保留小数点 只能四舍五入到整数

as.integer("4.5") integer本身就是取整数

as.numeric("three") The first line will convert the character string "4.5" to the integer 4. The second one will convert the character string "three" to an NA.

Instructions

  • var1 is logical. Convert it to character and assign it to the variable var1_char.

  • Next, see whether var1_char actually is a character by using the is.character()

character function on it.

  • var2 is numeric. Convert it logical and assign it to the variable var2_log.

  • Inspect the class of var2_log using class().

  • Finally, var3 is of type character. Convert it to numeric and assign the result to var3_num. Was it successful?

 

 


推荐文章
评论(0)
联系我们|招贤纳士|移动客户端|风格模板|官方博客|侵权投诉 Reporting Infringements|未成年人有害信息举报 0571-89852053|涉企举报专区
网易公司版权所有 ©1997-2024  浙公网安备 33010802010186号 浙ICP备16011220号-11 增值电信业务经营许可证:浙B2-20160599
网络文化经营许可证: 浙网文[2022]1208-054号 自营经营者信息 工业和信息化部备案管理系统网站 12318全国文化市场举报网站
网信算备330108093980202220015号 网信算备330108093980204230011号
分享到
转载我的主页