尝试在 R 中转换字符文件时出现问题——我似乎无法让 as.numeric() 正常工作
这是设置的问题:
我从机器学习存储库中读取了一个名为“abalone.data”的数据文件:
dat=read.csv(file="abalone.data",header=FALSE)
colnames(dat)<-c('Sex','Length','Diameter','Height','Whole weight',
'Shucked wieght','Viscera weight','Shell weight','Rings')
这是一个示例:
head(dat)
Sex Length Diameter Height Whole weight Shucked wieght Viscera weight Shell weight Rings
1 M 0.455 0.365 0.095 0.5140 0.2245 0.1010 0.150 15
2 M 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.070 7
3 F 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.210 9
这是结构”:
str(dat)
'data.frame': 4177 obs. of 9 variables:
$ Sex : chr "M" "M" "F" "M" ...
$ Length : num 0.455 0.35 0.53 0.44 0.33 0.425 0.53 0.545 0.475 0.55 ...
$ Diameter : num 0.365 0.265 0.42 0.365 0.255 0.3 0.415 0.425 0.37 0.44 ...
$ Height : num 0.095 0.09 0.135 0.125 0.08 0.095 0.15 0.125 0.125 0.15 ...
$ Whole weight : num 0.514 0.226 0.677 0.516 0.205 ...
$ Shucked wieght: num 0.2245 0.0995 0.2565 0.2155 0.0895 ...
$ Viscera weight: num 0.101 0.0485 0.1415 0.114 0.0395 ...
$ Shell weight : num 0.15 0.07 0.21 0.155 0.055 0.12 0.33 0.26 0.165 0.32 ...
$ Rings : int 15 7 9 10 7 8 20 16 9 19 ...
问题是:
我想转换第一行到数字;例如“M”到1,“F”到2,“I”到3。
所以,我尝试 Sex <- as.numeric(dat$Sex)
但我得到:
Sex<-as.numeric(dat$sex)
> Sex[1:5]
[1] NA NA NA NA NA
我已经尝试了很多类似的命令;例如:
as.numeric(dat$Sex=character(),levels=levels)
Error: unexpected '=' in " as.numeric(dat$Sex="
我无法弄清楚这一点。
请帮忙
Here is the question set up:
I have read in a data file from the Machine Learing Depository called "abalone.data":
dat=read.csv(file="abalone.data",header=FALSE)
colnames(dat)<-c('Sex','Length','Diameter','Height','Whole weight',
'Shucked wieght','Viscera weight','Shell weight','Rings')
Here is a sample:
head(dat)
Sex Length Diameter Height Whole weight Shucked wieght Viscera weight Shell weight Rings
1 M 0.455 0.365 0.095 0.5140 0.2245 0.1010 0.150 15
2 M 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.070 7
3 F 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.210 9
And here is the structure":
str(dat)
'data.frame': 4177 obs. of 9 variables:
$ Sex : chr "M" "M" "F" "M" ...
$ Length : num 0.455 0.35 0.53 0.44 0.33 0.425 0.53 0.545 0.475 0.55 ...
$ Diameter : num 0.365 0.265 0.42 0.365 0.255 0.3 0.415 0.425 0.37 0.44 ...
$ Height : num 0.095 0.09 0.135 0.125 0.08 0.095 0.15 0.125 0.125 0.15 ...
$ Whole weight : num 0.514 0.226 0.677 0.516 0.205 ...
$ Shucked wieght: num 0.2245 0.0995 0.2565 0.2155 0.0895 ...
$ Viscera weight: num 0.101 0.0485 0.1415 0.114 0.0395 ...
$ Shell weight : num 0.15 0.07 0.21 0.155 0.055 0.12 0.33 0.26 0.165 0.32 ...
$ Rings : int 15 7 9 10 7 8 20 16 9 19 ...
Here is the problem:
I want to convert the first row to numeric; e.g. "M" to 1, "F" to 2 and "I"to 3.
So, I try
Sex <- as.numeric(dat$Sex)
but I get:
Sex<-as.numeric(dat$sex)
> Sex[1:5]
[1] NA NA NA NA NA
I've tried a lot of similar commands; e.g.:
as.numeric(dat$Sex=character(),levels=levels)
Error: unexpected '=' in " as.numeric(dat$Sex="
I cannot figure this out.
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 Sex 变量是一个字符向量。您首先需要将其更改为一个因子:
That's because the Sex variable is a character vector. You first need to change it to a factor: