简体   繁体   English

R 将列表转换为矩阵

[英]R Convert list to matrix

I am trying to create a matrix from a set of list of characters as follows:我正在尝试从一组字符列表创建一个矩阵,如下所示:

list <- list("0010","0110","1000")

I would like to convert this to a matrix with 4 columns (for each of each character of each element of the list) and 3 rows like:我想将其转换为具有 4 列(对于列表中每个元素的每个字符)和 3 行的矩阵,例如:

在此处输入图片说明

And now I need the numbers (0s and 1s) to be recognized as numbers.现在我需要将数字(0 和 1)识别为数字。

Thanks for your help!谢谢你的帮助!

EDIT with the output of my data, I can see now why I got that error.使用我的数据输出进行编辑,我现在可以看到为什么会出现该错误。 By the way, thank you so much for providing the answers.顺便说一句,非常感谢您提供答案。 It looks like when using the scan function it added the name of the file as well to the list (I don't know how to fix this:看起来在使用扫描功能时,它也将文件名添加到列表中(我不知道如何解决这个问题:

第一个元素

第二个元素

From your question, it's not quite clear if you actually mean to use a list or a vector;从您的问题来看,您实际上是要使用列表还是向量还不是很清楚; also, I would avoid naming variables the same as function names like list .另外,我会避免将变量命名为与list等函数名称相同的名称。 Here's how you can solve:以下是您可以解决的方法:

# Try it with l as a list
l <- list("0010","0110","1000")
t(sapply(l, function(x) as.numeric(unlist(strsplit(x, '')))))
#>      [,1] [,2] [,3] [,4]
#> [1,]    0    0    1    0
#> [2,]    0    1    1    0
#> [3,]    1    0    0    0
# Try it with l as a vector
l <- c("0010","0110","1000")
t(sapply(l, function(x) as.numeric(unlist(strsplit(x, '')))))
#>      [,1] [,2] [,3] [,4]
#> 0010    0    0    1    0
#> 0110    0    1    1    0
#> 1000    1    0    0    0

Created on 2018-11-09 by the reprex package (v0.2.1)reprex 包(v0.2.1) 于 2018 年 11 月 9 日创建

Explanation解释

sapply(x, fun) applies function fun to every element of x . sapply(x, fun)将函数fun应用于x每个元素。 So,所以,

sapply(l, function(x) as.numeric(unlist(strsplit(x, ''))))

takes every element of l , uses strsplit(x, '') to get every individual character from that element (each "0" or "1" ), then we must unlist() because strsplit() returns a list, wrap in as.numeric() since you want numbers, and we have to wrap all of that in t() since when sapply() returns a matrix, it does it by column.获取l每个元素,使用strsplit(x, '')从该元素中获取每个单独的字符(每个"0""1" ),然后我们必须unlist()因为strsplit()返回一个列表,换as.numeric()因为你想要数字,我们必须将所有这些都包装在t()因为当sapply()返回一个矩阵时,它是按列进行的。

Update更新

From your updated question, it appears that your list elements are not in character form at all.从您更新的问题来看,您的列表元素似乎根本不是字符形式。 In that case, I would follow the advice of a now deleted answer and use Reduce() and rbind()在这种情况下,我会遵循现已删除的答案的建议并使用Reduce()rbind()

l <- list('digist/test_digits/0_0.txt' = c(0, 0, 1, 0),
          'digist/test_digits/0_1.txt' = c(0, 1, 1, 0),
          'digist/test_digits/1_1.txt' = c(1, 0, 0, 0))
l
#> $`digist/test_digits/0_0.txt`
#> [1] 0 0 1 0
#> 
#> $`digist/test_digits/0_1.txt`
#> [1] 0 1 1 0
#> 
#> $`digist/test_digits/1_1.txt`
#> [1] 1 0 0 0
Reduce('rbind', l)
#>      [,1] [,2] [,3] [,4]
#> init    0    0    1    0
#>         0    1    1    0
#>         1    0    0    0

Created on 2018-11-09 by the reprex package (v0.2.1)reprex 包(v0.2.1) 于 2018 年 11 月 9 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM