简体   繁体   English

R中基于矩阵名称的带有循环的R中的多个矩阵运算

[英]Multiple Matrix Operations in R with loop based on matrix name

I'm a novice R user, who's learning to use this coding language to deal with data problems in research. 我是R新手,正在学习使用这种编码语言来处理研究中的数据问题。 I am trying to understand how knowledge evolves within an industry by looking at patenting in subclasses. 我试图通过查看子类中的专利来了解知识如何在一个行业中发展。 So far I managed to get the following: 到目前为止,我设法做到了以下几点:

# kn.matrices<-with(patents, table(Class,year,firm))
# kn.ind <- with(patents, table(Class, year))

patents is my datafile, with Subclass, app.yr, and short.name as three of the 14 columns 专利是我的数据文件,在14列中有3列是Subclass,app.yr和short.name

# for (k in 1:37)  
# kn.firms = assign(paste("firm", k ,sep=''),kn.matrices[,,k]) 

There are 37 different firms (in the real dataset, here only 5) 有37个不同的公司(在实际数据集中,这里只有5个)

This has given 37 firm-specific and 1 industry-specific 2635 by 29 matrices (in the real dataset). 这样就得到了29个矩阵(在实际数据集中)的37个特定公司和1个特定行业的2635。 All firm-specific matrices are called firmk with k going from 1 until 37. 所有特定于公司的矩阵都称为firmk,k从1到37。

I would like to perform many operations in each of the firm-specific matrices (eg compare the numbers in app.yr 't' with the average of the 3 previous years across all rows) so I am looking for a way that allows me to loop the operations for every matrix named firm1,firm2,firm3...,firm37 and that generates new matrices with consistent naming, eg firm1.3yearcomparison 我想在每个公司特定的矩阵中执行许多操作(例如,将app.yr't'中的数字与所有行中前三年的平均值进行比较),因此我正在寻找一种方法,使我能够循环每个名为firm1,firm2,firm3 ...,firm37的矩阵的运算,并生成命名一致的新矩阵,例如firm1.3yearparison

Hopefully I framed this question in an appropriate way. 希望我以适当的方式提出这个问题。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Following comments I'm trying to add a minimal reproducible example 在评论之后,我尝试添加一个最小的可复制示例

year<-c(1990,1991,1989,1992,1993,1991,1990,1990,1989,1993,1991,1992,1991,1991,1991,1990,1989,1991,1992,1992,1991,1993) 年<-c(1990,1991,1989,1992,1993,1991,1990,1990,1989,1993,1991,1992,1991,1991,1991,1990,1989,1991,1992,1992,1992,1991,1993)

firm<-(c("a","a","a","b","b","c","d","d","e","a","b","c","c","e","a","b","b","e","e","e","d","e")) firm <-(c(“ a”,“ a”,“ a”,“ b”,“ b”,“ c”,“ d”,“ d”,“ e”,“ a”,“ b”, “ c”,“ c”,“ e”,“ a”,“ b”,“ b”,“ e”,“ e”,“ e”,“ d”,“ e”)))

class<-c(1900,2000,3000,7710,18000,19000,36000,115000,212000,215000,253600,383000,471000,594000) 类别<-c(1900,2000,3000,7710,18000,19000,36000,115000,212000,215000,253600,383000,471000,594000)

These three vectors thus represent columns in a spreadsheet that forms the "patents" matrix mentioned before. 因此,这三个向量表示电子表格中的列,该电子表格形成了前面提到的“专利”矩阵。

it looks like you already have a 3 dimensional array with all your data. 看起来您已经拥有一个包含所有数据的3维数组。 You can basically view this as your 38 matrices all piled one on top of the other. 您基本上可以将其视为38个矩阵,一个矩阵堆叠在另一个矩阵之上。 You don't want to split this into 38 matrices and use loops. 您不想将其拆分为38个矩阵并使用循环。 Instead, you can use R's apply function and extraction functions. 相反,您可以使用R的apply函数和提取函数。 Just view the help topic on the apply() family and it should show you how to do what you want. 只需查看apply()系列上的帮助主题,它就会向您展示如何做您想做的事情。 Here are a few basic examples 以下是一些基本示例

examples: 例子:

# returns the sums of all columns for all matrices
apply(kn.matrices, 3, colSums)

# extract the 5th row of all matrices
kn.matrices[5, , ]

# extract the 5th column of all matrices
kn.matrices[, 5, ]

# extract the 5th matrix
kn.matrices[, , 5]

# mean of 5th column for all matrices
colMeans(kn.matrices[, 5, ])

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

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