list 在 R 中列出整数或双精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3814322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List to integer or double in R
提问by user446667
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?
我有一个大约 1000 个单个整数的列表。我需要能够进行一些数学计算,但它们被困在列表或字符形式中。我怎样才能切换它们以便它们可用?
sample data:
样本数据:
> 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" ...
Just the first couple of lines.
只是前几行。
回答by Joris Meys
See ?unlist :
看到 ?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.
如果这不能解决您的问题,请具体说明您想要做什么。
edit : It's even simpler apparently :
编辑:显然更简单:
> x <- list(as.character(1:3))
> x
[[1]]
[1] "1" "2" "3"
> y <-as.numeric(x[[1]])
> y
[1] 1 2 3
回答by Dirk Eddelbuettel
Try this -- combining as.numeric()
and rbind()
:
试试这个——结合as.numeric()
和rbind()
:
> foo <- list("2", "4", "7")
> foo
[[1]]
[1] "2"
[[2]]
[1] "4"
[[3]]
[1] "7"
> bar <- do.call(rbind, lapply(foo, as.numeric))
> bar
[,1]
[1,] 2
[2,] 4
[3,] 7
>