为什么转置函数在 R 中将数字更改为字符? [英] Why does the transpose function change numeric to character in R?

查看:34
本文介绍了为什么转置函数在 R 中将数字更改为字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Excel 中构建了一个简单的矩阵,其中包含一些字符值和一些数值(屏幕截图Excel 中设置的数据).我使用 openxlsx 包将其读入 R,如下所示:

库(openxlsx)数据 <- read.xlsx('~desktop/data.xlsx)

然后我检查类:

sapply(data, class)x1 a b c字符"数字"数字"数字"

这正是我想要的.当我尝试转置矩阵,然后再次检查类时,我的问题出现了:

data <- t(data)

当我现在检查 sapply 时,所有值都是字符".为什么转置时不保留类?

解决方案

首先,当我阅读您的电子表格时,我没有得到您的结果,因为以逗号分隔的数字的单元格显示为字符.

>

data <- read.xlsx("data.xlsx")数据# X1 a b c#1 x 0,1 3 4,5#2 y 2,4 0 6,5#3 z 24 0 0应用(数据,类)# X1 a b c#"字符" "字符" "数字" "字符"

但您真正看到的问题是,通过转置数据框,您将在同一列中混合类型,因此 R 必须将整个列转换为最广泛的常见类型,在本例中为字符.

mydata<-data.frame(X1=c("x","y","z"),a=c(1,2,24),b=c(3,0,0),c=c(4,6,0),stringsAsFactors = FALSE)应用(我的数据,类)# X1 a b c#字符"数字"数字"数字"#你展示了什么t(我的数据)# [,1] [,2] [,3]#X1 "x" "y" "z"#a " 1" " 2" "24"#b "3" "0" "0"#c "4" "6" "0"mydata_t<-t(mydata)应用(mydata_t,类)# x 1 3 4 y 2 #0 6 z 24#人物"人物"人物"人物"人物"人物"#人物"人物"人物"人物"# 0 0#"性格" "性格"

您想处理转置矩阵中的数字并将它们转置回来吗?如果是这样,转置一个临时删除了字符列的子矩阵,然后重新组合,如下所示:

sub_matrix<-t(mydata[,-1])子矩阵# [,1] [,2] [,3]#a 1 2 24#b 3 0 0#c 4 6 0sub_matrix2<-sub_matrix*2子矩阵2# [,1] [,2] [,3]#a 2 4 48#b 6 0 0#c 8 12 0cbind(X1=mydata[,1],as.data.frame(t(sub_matrix2)))# X1 a b c#1 x 2 6 8#2 y 4 0 12#3 z 48 0 0

I've constructed a simple matrix in Excel with some character values and some numeric values (Screenshot of data as set up in Excel). I read it into R using the openxlsx package like so:

library(openxlsx)
data <- read.xlsx('~desktop/data.xlsx)

After that I check the class:

sapply(data, class)
         x1         a         b          c
"character" "numeric" "numeric"  "numeric"

Which is exactly what I want. My problem occurs when I try to transpose the matrix, and then check for class again:

data <- t(data)

When i check with sapply now, all values are "character". Why are the classes not preserved when transposing?

解决方案

First off, I don't get your result when I read in your spreadsheet due to the fact the the cells with comma separated numbers appear as characters.

data <- read.xlsx("data.xlsx")
data
#  X1   a b   c
#1  x 0,1 3 4,5
#2  y 2,4 0 6,5
#3  z  24 0   0
sapply(data,class)
#         X1           a           b           c 
#"character" "character"   "numeric" "character" 

But the issue you are really seeing is that by transposing the data frame you are mixing types in the same column so R HAS TO convert the whole column to the broadest common type, which is character in this case.

mydata<-data.frame(X1=c("x","y","z"),a=c(1,2,24),b=c(3,0,0),c=c(4,6,0),stringsAsFactors = FALSE)
sapply(mydata,class)
#         X1           a           b           c 
#"character"   "numeric"   "numeric"   "numeric" 
# what you showed
t(mydata)
#   [,1] [,2] [,3]
#X1 "x"  "y"  "z" 
#a  " 1" " 2" "24"
#b  "3"  "0"  "0" 
#c  "4"  "6"  "0" 

mydata_t<-t(mydata)
sapply(mydata_t,class)
#          x           1           3           4           y           2           #0           6           z          24 
#"character" "character" "character" "character" "character" "character" #"character" "character" "character" "character" 
#          0           0 
#"character" "character" 

Do you want to work on the numbers in the transposed matrix and transpose them back after? If so, transpose a sub-matrix that has the character columns temporarily removed, then reassemble later, like so:

sub_matrix<-t(mydata[,-1])
sub_matrix
#  [,1] [,2] [,3]
#a    1    2   24
#b    3    0    0
#c    4    6    0
sub_matrix2<-sub_matrix*2
sub_matrix2
#  [,1] [,2] [,3]
#a    2    4   48
#b    6    0    0
#c    8   12    0
cbind(X1=mydata[,1],as.data.frame(t(sub_matrix2)))
#  X1  a b  c
#1  x  2 6  8
#2  y  4 0 12
#3  z 48 0  0

这篇关于为什么转置函数在 R 中将数字更改为字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆