列出R中的整数或双精度 [英] List to integer or double in R

查看:38
本文介绍了列出R中的整数或双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约1000个单整数的列表.我需要能够进行一些数学计算,但是它们被卡在列表或字符形式中.我该如何切换它们以使其可用?

I have a list of about 1000 single integers. I need to be able to do some mathematical computations, but they're stuck in list or character form. How can I switch them so they're usable?

样本数据:

> y [[1]] 
  [1] "7" "3" "1" "6" "7" "1" "7" "6" "5" "3" "1" "3" "3" "0" "6" "2" "4" "9" 
  [19] "1" "9" "2" "2" "5" "1" "1" "9" "6" "7" "4" "4" "2" "6" "5" "7" "4" "7"     
  [37] "4" "2" "3" "5" "5" "3" "4" "9" "1" "9" "4" "9" "3" "4" "9" "6" "9" "8" 
  [55] "3" "5" "2" "0" "3" "1" "2" "7" "7" "4" "5" "0" "6" "3" "2" "6" "2" "3" 
  [73] "9" "5" "7" "8" "3" "1" "8" "0" "1" "6" "9" "8" "4" "8" "0" "1" "8" "6" ...

只是前几行.

推荐答案

请参见?unlist:

See ?unlist :

> x
[[1]]
[1] "1"

[[2]]
[1] "2"

[[3]]
[1] "3"


> y <- as.numeric(unlist(x))

> y
[1] 1 2 3

如果这不能解决您的问题,请具体说明您要做什么.

If this doesn't solve your problem, please specify what exactly you want to do.

显然,它甚至更简单:

> x <- list(as.character(1:3))

> x
[[1]]
[1] "1" "2" "3"


> y <-as.numeric(x[[1]])

> y
[1] 1 2 3

这篇关于列出R中的整数或双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆