数据框从数字变为字符 [英] Data frame changes from numeric to character

查看:129
本文介绍了数据框从数字变为字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开我的csv文件,我控制每个数据的类:

  mydataP <-read.csv energy_protein2.csv,stringsAsFactors = F)

apply(mydataP,2,function(i)class(i))
# code>

我添加一个列并检查数据类:

  mydataP [,ID]< -rep(c(KOH1,KOH2,KOH3,KON1,KON2,KON3,WTH1 ,WTH2,WTH3,WTN1,WTN2,WTN3),每个= 2)

apply(mydataP,2,function b $ b

这里改为字符

  as.numeric(as.factor(mydataP))
sort.list(y)中的#Error:'x'对于'sort.list'必须是原子
#你在列表上叫排序吗?

as.numeric(as.character(mydataP))



一个有117 NA的向量



我不知道现在该做什么,只要我触摸框架它改变为字符,有人可以帮助我吗?感谢

解决方案

发生这种情况是因为 apply



请尝试这样做:

  sapply(mydataP,class)

这是你通常应该避免在上使用 apply 的原因。data.frame s。



此行为记录在帮助文件中(?apply ):



< >

如果X不是数组,而是具有非null dim
值(如数据框)的类的对象,则应用尝试将其强制到数组
通过asmatrix如果它是二维的(例如,数据框)或通过
as.array。







以下是内置虹膜数据集的可重复示例:

  
#Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#charactercharactercharactercharacter character

> sapply(iris,class)
#Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#numericnumericnumericnumericfactor

> str(iris)
#'data.frame':150 obs。的5个变量:
#$ Sepal.Length:num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#$ Sepal.Width:num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1。 ..
#$ Petal.Length:num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#$ Petal.Width:num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#$ Species:Factor w / 3 levelssetosa,versicolor,..:1 1 1 1 1 1 1 1 1 1 ...
pre>

如您所见, apply 将所有列转换为同一个类。


I open my csv file and I control the class of each of my data:

mydataP<-read.csv("Energy_protein2.csv", stringsAsFactors=F) 

apply(mydataP, 2, function(i) class(i))
#[1] "numeric" 

I add a column and check the class of the data:

mydataP[ ,"ID"] <-rep(c("KOH1", "KOH2", "KOH3", "KON1", "KON2", "KON3", "WTH1", "WTH2", "WTH3","WTN1", "WTN2", "WTN3"), each=2)

apply(mydataP, 2, function(i) class(i))

Here it changes to a "character"

as.numeric(as.factor(mydataP))
#Error in sort.list(y) : 'x' must be atomic for 'sort.list'
#Have you called 'sort' on a list?

as.numeric(as.character(mydataP))

I get a vector with 117 NA

I have no idea what to do now, as soon I touch the frame it changes to character, can somebody help me? Thanks

解决方案

That happens because apply converts your data.frame to matrix and those can only have one class in them.

Try this instead:

sapply(mydataP, class)

This is the reason you should normally try to avoid using apply on data.frames.

This behavior is documented in the help file (?apply):

If X is not an array but an object of a class with a non-null dim value (such as a data frame), apply attempts to coerce it to an array via as.matrix if it is two-dimensional (e.g., a data frame) or via as.array.


Here's a reproducible example with the built-in iris dataset:

> apply(iris, 2, function(i) class(i))
#Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
# "character"  "character"  "character"  "character"  "character" 

> sapply(iris, class)
#Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#   "numeric"    "numeric"    "numeric"    "numeric"     "factor" 

> str(iris)
#'data.frame':  150 obs. of  5 variables:
# $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
# $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
# $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
# $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

As you can see, apply converts all columns to the same class.

这篇关于数据框从数字变为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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