如何设置data.frame的行名? [英] How to set rownames of a data.frame?
问题描述
在创建data.frame时遇到了一个问题。当我可以创建 data.frame
并设置适当的行和列名称时。
结果如下所示:
row.names非洲ALPS比荷卢经济联盟
CS MRI系统92151130
CS计算为9411091
CS Interv X射线548535614
编辑:看到提交后,我的格式丢失了。表示数据框的左上角显示为row.names(RStudio)。
这是预期的。我想将此写入excel(write.xlsx),并且也可以。但是,在Excel中,单元格A1现在为空。相反,我想在该单元格中输入月份的名称。
所以问题变成了:
- 我可以更改data.frame以便将上面示例中的row.names替换为April吗?
- 如果不可能的话,写完此后如何到excel工作表中,只将单元格A1更改为四月吗?
我尝试了两种方法的搜索,但找不到解决方案。 / p>
为什么要这个btw的原因是,如果我从该工作表中读取并再次存储在data.frame中,则它会从excel文件中排除row.names
您的问题没有包含足够的详细信息。但基本上:
-
要在数据框
dd
中设置行名,使用行名(dd)= 1:nrow(dd)
-
要写入csv文件,但不包括行名,请使用
write.csv(dd,file = output.csv,row.names = FALSE)
-
要写入xlsx文件,但不包括行名,请使用
write.xlsx( dd,file = output.xlsx,row.names = FALSE)
I came across an issue when creating a data.frame. When I can create a data.frame
, and set the appropriate row and column names.
Results looks like this:
row.names Africa ALPS Benelux
CS MRI Systems 92 151 130
CS Computed To 94 110 91
CS Interv X-Ray 548 535 614
Edit: see that after submitting, my formatting gets lost. Means to show that top left part of the data frame says row.names (RStudio).
This is as intended. I want to write this to excel (write.xlsx), and also that works. However, in the excel, cell A1 is now empty. Instead, I would like to put the name of the month in that cell.
So the question becomes:
- Can I change the data.frame such that row.names in the above example is replaced by April?
- If not possible, how can I after writing this to an excel sheet, change only cell A1 to April?
I have tried searching on both methods, but cannot find the solution.
Reason why I want this btw, is that if I read from that sheet, and store in a data.frame again, it excludes the row.names from the excel file if the cell A1 is empty.
Your question doesn't contain enough detail. But basically:
To set rownames in the data frame
dd
, userownames(dd) = 1:nrow(dd)
To write to a csv file, but exclude the row names, use
write.csv(dd, file="output.csv", row.names=FALSE)
To write to a xlsx file, but exclude the row names, use
write.xlsx(dd, file="output.xlsx", row.names=FALSE)
这篇关于如何设置data.frame的行名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!