[英]Naming the rows in a matrix in R
I am trying to name the rows in a matrix, but it adds the prefixes 'X.', 'X..' etc. in front of these names.我试图命名矩阵中的行,但它在这些名称前面添加了前缀“X.”、“X..”等。 Also, the row names don't come out properly.
此外,行名称显示不正确。 For example, the first-row name is supposed to be 'e subscript (t+1)' but it shows something else.
例如,第一行名称应该是“e 下标 (t+1)”,但它显示了其他内容。 The even-numbered row names should be vacant, but they are given names.
偶数行名称应该是空的,但它们是给定的名称。 Could you help please?
你能帮忙吗?
Please see the dataset here.请在此处查看数据集。
Below is the code I used:下面是我使用的代码:
rownames(table2a)=paste(c("$e_t+1$"," ","$r_t+1$", " ","$\\Delta y_{n,t+1}$"," ",
"$s_{n,t+1}$"," ", "$d_t+1-p_t+1$"," ", "$rb_t+1$"," "))
Included libraries: matrix
, dplyr
, tidyverse
, xtable
.包含的库:
matrix
、 dplyr
、 tidyverse
、 xtable
。
Below is the data from dput(table2a)
:以下是来自
dput(table2a)
的数据:
structure(c(-0.011918875562309, 0.0493186644094629, 0.00943711646402318,
0.0084043692395113, 0.0140061617086464, 0.00795790133389348,
-0.00372516684283399, 0.00631517247007723, 0.00514156266584497,
0.0039339752041611, 0.0148362913561212, 0.00793003246354337,
-0.0807656037164587, 0.0599852917766847, 0.991792361285981, 0.0102220639400435,
-0.00608828061911691, 0.00967903407684488, 0.010343002117867,
0.00768101625973846, 0.0578541455030235, 0.00478481429473926,
-0.00902328873121743, 0.00964513773477125, -0.799680080407018,
0.340494864072598, 0.0519648273240202, 0.0580235615884655, 0.0850517813830584,
0.0549411579861702, -0.0665428760388874, 0.0435997977143392,
-0.032698572959578, 0.027160069487786, 0.114163705951583, 0.0547487519805466,
0.352025366916776, 0.197746547959218, 0.0476825327079758, 0.0336978915546042,
0.0464511908714403, 0.0319077480426568, 0.904849333951824, 0.0253211146465119,
0.132904050913606, 0.0157735418364402, 0.0653710645280691, 0.0317960059066269,
0.939695537568421, 0.612311426298072, -0.0578948128653228, 0.104343687684969,
-0.0744692071400603, 0.0988006057025484, 0.121089017775182, 0.0784054537723728,
0.0345069733304992, 0.048841914052704, -0.090885199308955, 0.0984546022582597,
-0.280821673428002, 0.248826811381596, -0.0288068135696716, 0.0424024540117092,
-0.0239685609446809, 0.0401498953370305, 0.00219488911775388,
0.0318618569231297, 0.066433933135983, 0.0198480335553826, 0.871940074366622,
0.0400092888905855), .Dim = c(12L, 6L), .Dimnames = list(c("$e_t+1$",
" ", "$r_t+1$", " ", "$\\Delta y_{n,t+1}$", " ", "$s_{n,t+1}$",
" ", "$d_t+1-p_t+1$", " ", "$rb_t+1$", " "), c("ex_stock_ret_100.l1",
"real_int_100.l1", "Chg_1month.l1", "spreads.l1", "log_dp.l1",
"rb_rate_100.l1")))
My desired output (row & column names) is as shown in this picture enter image description here我想要的输出(行和列名称)如图所示在此处输入图像描述
如果要删除该前缀,可以执行以下操作:
rownames(table2a) <- substring(rownames(table2a), 2)
you can remove the first X and all following dots (no matter how many there are) with the gsub command:您可以使用 gsub 命令删除第一个 X 和所有后续点(无论有多少):
rownames(table2a) <- gsub("^X\\.*","",rownames(table2a))
^
= beginning of the string; ^
= 字符串的开头; X
= your actual X; X
= 你的实际 X; \\\\.
= a dot; = 一个点;
*
= 0 or more of the before mentioned (in this case \\\\.
); *
= 0 个或多个前面提到的(在本例中为\\\\.
); so in total ^X\\\\.
所以总共
^X\\\\.
means: if you find X as the first letter and all possible dots following directly behind it.意思是:如果你发现 X 作为第一个字母,所有可能的点紧跟在它后面。
gsub replaces this find with "", meaning nothing, leaving only whatever comes after gsub 用 "" 替换这个发现,意味着什么都没有,只留下后面的
EDIT: to also get rid of every 2nd rowname, add a little something extra:编辑:还要去掉每个第二行名,添加一些额外的东西:
rownames(table2a) <- gsub("^X\\.*[1-9]*","",rownames(table2a))
which gets rid of any number directly behind the dots.它摆脱了直接在点后面的任何数字。 This should leave those rows empty.
这应该将这些行留空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.