且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串转换为数字

更新时间:2023-02-03 08:20:28

我怀疑您对因子有问题.例如,

I suspect you are having a problem with factors. For example,

> x = factor(4:8)
> x
[1] 4 5 6 7 8
Levels: 4 5 6 7 8
> as.numeric(x)
[1] 1 2 3 4 5
> as.numeric(as.character(x))
[1] 4 5 6 7 8

一些评论:

  • 您提到您的向量包含字符Down"和NoData".期望/希望 as.numeric 对这些值做什么?
  • read.csv 中,尝试使用参数 stringsAsFactors=FALSE
  • 你确定是 sep="/t 而不是 sep="\t"
  • 使用命令head(pitchman)检查数据的前几行
  • 此外,当您不提供数据时,很难猜测您的问题是什么.最小的工作示例总是可取的.例如,我不能运行命令 pichman <- read.csv(file="picman.txt", header=TRUE, sep="/t") 因为我没有访问数据集.
  • You mention that your vector contains the characters "Down" and "NoData". What do expect/want as.numeric to do with these values?
  • In read.csv, try using the argument stringsAsFactors=FALSE
  • Are you sure it's sep="/t and not sep="\t"
  • Use the command head(pitchman) to check the first fews rows of your data
  • Also, it's very tricky to guess what your problem is when you don't provide data. A minimal working example is always preferable. For example, I can't run the command pichman <- read.csv(file="picman.txt", header=TRUE, sep="/t") since I don't have access to the data set.