R:创建一个桌子,其中单元格在RBIND之后具有1或0个小数位置



我正在在R中创建一个表。某些行中的值应该具有0个小数点(例如人数(,而其他行中的值应具有1个小数位置(例如,人口(。

i具有数据框架,然后使用圆函数在R中的圆形功能创建两个表 - 圆形0(0小数点位置(和圆形1(1个小数点(。

round1<-round(prof.table[-c(2:3),], 1)
round0<-round(prof.table[2:3,], 0)
prof.table<-rbind(round0, round1)

我将它们结合在一起后,我希望从圆形表中的值中的值将零十进制位置,而1号回合的值将有1个小数位。但是,在rbind之后,所有单元格中的值都有1个小数位,因此我的整数显示为nnn.0。如何从整数中删除这个多余的小数位?

您正在尝试将numeric值与integer组合。向量(或data.frame列(只能有一个类。它要么迫使数字为整数,要么将整数为数字。鉴于此选择,后者是可取的,因为从转换22.0000没有数据丢失。

这将有助于解释类的差异:整数类和r

中的数字类别有什么区别

一个例子:

# create an integer vector x (0 decimal places) & numeric vector y (>0 decimal places)
x <- as.integer(1:3)
y <- runif(3)
# check their classes to confirm
class(x)
class(y)
# bind them together, and view class
z <- c(x, y)
z
class(z)

由于我需要在表中显示的数据时,我遵循@RUI的建议将数据胁到角色格式,并感谢@Jonny的提示,即向量只能是一堂课:


#Round certain variables to one decimal point
round1<-round(prof.table[-c(2:3),], 1) 
#set as character
round1$`V1`<-as.character(round1$`V1`) 
round1$`V2`<-as.character(round1$`V2`) 
#Round others to zero decimal point
round0<-round(prof.table[2:3,], 0) 
#set them as character
round0$`V1`<-as.character(round0$`V1`) 
round0$`V2`<-as.character(round0$`V2`) 
#combine into data frame
prof.table<-rbind(round0, round1) 

最新更新