假设我有一个数据框:
df <- data.frame(A = c(17.1, 0.0, 0.0, 0.0),
B = c(22.2, 0.0, 0.0, 0.0),
C = c(2.4, 5.5, 1.8, 3.3))
A B C
1 17.1 22.2 2.4
2 0.0 0.0 5.5
3 0.0 0.0 1.8
4 0.0 0.0 3.3为了使用View()查看,我希望在第2-4行有空白条目,其中我有0.0个元素。以下是所需的输出:
A B C
1 17.1 22.2 2.4
2 5.5
3 1.8
4 3.3一种方法是将df列转换为字符,并将“0.0”字符设置为“”。理想情况下,如果可能的话,我希望非零元素保持数值型。有没有更好的方法来做这件事?
发布于 2019-03-02 01:39:44
可以使用format函数中的zero.print选项打印数据框。
print(format(df, zero.print=""))
A B C
1 17.1 22.2 2.4
2 5.5
3 1.8
4 3.3另一种选择是使用knitr包中的kable函数。
knitr::kable(df, format.args= list(zero.print = ""))
| A| B| C|
|----:|----:|---:|
| 17.1| 22.2| 2.4|
| | | 5.5|
| | | 1.8|
| | | 3.3|https://stackoverflow.com/questions/54949362
复制相似问题