好的,所以我需要一个非常具体的子集化公式.从矩阵x,我只想保留由行和列定义的元素.然后应该用零替换不需要的元素.示例如下:
> x <- matrix(c(65,46,52,76,34,345,65,87,12,53),nrow = 5,ncol = 2)
> x
[,1] [,2]
[1,] 65 345
[2,] 46 65
[3,] 52 87
[4,] 76 12
[5,] 34 53
> rows <- c(1,1,2,3,3,4,5)
> cols <- c(1,2,2,1,2,1,2)
Run Code Online (Sandbox Code Playgroud)
魔法
> x
[,1] [,2]
[1,] 65 345
[2,] 0 65
[3,] 52 87
[4,] 76 0
[5,] 0 53
Run Code Online (Sandbox Code Playgroud)
非常感谢
我有这个角色矢量:
variables <- c("ret.SMB.l1", "ret.mkt.l1", "ret.mkt.l4", "vix.l4", "ret.mkt.l5" "vix.l6", "slope.l11", "slope.l12", "us2yy.l2")
Run Code Online (Sandbox Code Playgroud)
期望的输出:
> suffixes(variables)
[1] 1 1 4 4 5 6 11 12 2
Run Code Online (Sandbox Code Playgroud)
换句话说,我需要一个函数来返回一个显示后缀的数字向量(每个后缀都是1或2位长).请注意,我需要一些可以使用更多数量的字符串的东西,这些字符串在中间可能有也可能没有数字.数字后缀的范围为1到99.
非常感谢