用R中的两个for循环创建一个矩阵/数据框 [英] creating a matrix/dataframe with two for loops in R

查看:823
本文介绍了用R中的两个for循环创建一个矩阵/数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我关于SO的第一篇文章,所以请客气!

This is my first post on SO, so be kind!

我的问题与此模糊相关:
在R中创建矩阵的double for循环

My question is vaguely related to this one: Double for loop in R creating a matrix

我想创建一个矩阵/数据框,我脑海中选择的方法是嵌套两个for循环,一个用于创建第一行,第二个对我需要的行重复。

I want to create a matrix/dataframe and the approach my mind has chosen is to nest two for loops, one to create the first row and the second to repeat it for the rows I nedd.

我可以成功创建第一个循环,但是对于需要的行数,我似乎无法对其进行迭代。

I could successfully create the first loop, but I can't seem to iterate it for the number of rows I need.

I '肯定有一种更好的方法可以执行此操作,无论如何,这是for循环,可为我提供第一行所需的结果:

I'm sure that there is a better way to do this, anyway, this is the for loop that gives the result I need for the first row:

x <- character(0)
for(j in 1:18){
    x <- c(x, sum(it_mat[1, 2:26] == j))
}

it_mat是417行26列的矩阵,其中第一个column是具有各种名称和后续列的字符串向量ns是从1到18的随机数。

it_mat is a matrix of 417 rows and 26 columns, where the first column is a string vector with various names and the subsequent columns are randomly generated numbers from 1 to 18.

这是第一行:

[1,] "Charlie" "14" "3"  "9"  "14" "3"  "9"  "11" "11" "18"  "17"  "16"  "5"   "18"  "6"   "10"  "3"   "9"   "9"   "3"   "18"  "12"  "8"   "5"   "5"  "4"

我想创建一个矩阵/ df,在其中我计算每个名字每个数字出现多少次。

I want to create a matrix/df where I count how many times, for each name, each number appearead.

我在上面创建的for循环为第一行提供了想要的结果:

The for loop I created above gives me the result I want for the first row:

x
[1] "0" "0" "4" "1" "3" "1" "0" "1" "4" "1" "2" "1" "0" "2" "0" "1" "1" "3"

我真的不能用另一个for循环为随后的行迭代,肯定有一些我常做错的事情。

I really can't iterate it for the subsequent rows with another for loop, there must be something very mundane that I do wrong.

这是我的最佳尝试:

tr_mat <- matrix(, nrow = 147, ncol = 18)
for(i in 1:147){
    x <- character()
    for(j in 1:18){
        x <- c(x, sum(it_mat[i, 2:26] == j))
    }
    tr_mat <- rbind(tr_mat, x)
}

我整个下午都在忙着,现在我放弃并与您联系,在您给我正确的方法之前,请解释一下嵌套循环中我做错了什么尝试,我可能会学到一些东西。

I went on it all afternoon and now I give up and reach out to you, before you give me the correct way to do it, please explain what I'm doing wrong in the nested for loops try, I might learn something.

我希望我能向自己解释,对不起,如果我太冗长。
谢谢您的时间。

I hope I explained myself, sorry if I've been too verbose. Thanks for your time.

推荐答案

使用底数R的另一种方法。请注意, * apply 函数是伪装的循环。

Another way, using base R. Note that *apply functions are loops in disguise.

tr_mat2 <- sapply(1:18, function(j) sapply(1:147, function(i) sum(it_mat[i, 2:26] == j)))

请注意,此代码将生成数字矩阵,而您的 tr_mat 的模式为字符

Note that this code will produce a matrix of numbers, while your tr_mat is of mode character:

all.equal(tr_mat, tr_mat2)
#[1] "Modes: character, numeric"

DATA。

这是数据集生成代码我曾经用来测试上面的代码。

DATA.
This is the dataset generation code that I have used to test the code above.

set.seed(7966)    # make the results reproducible
it_mat <- t(replicate(147, c(sample(letters, 1), sample(18, 25, TRUE))))

编辑。

按照MKR注释中的建议,以下是我对t进行修改后对OP的代码进行的更正

EDIT.
Following the suggestion in the comments by MKR, here is the OP's code corrected with my modification in the comment to his (the OP's) post.

tr_mat <- matrix(, nrow = 147, ncol = 18)
for(i in 1:147){
    x <- character()
    for(j in 1:18){
        x <- c(x, sum(it_mat[i, 2:26] == j))
    }
    tr_mat[i, ] <- x
}

这是我用来生成矩阵 tr_mat 的代码,在所有中均已引用上面的.equal 测试。

This is the code that I have used to produce the matrix tr_mat refered to in the all.equal test above.

这篇关于用R中的两个for循环创建一个矩阵/数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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