读取表格 '1.4523e-9' 中的数据
Read data in form '1.4523e-9'
我正在尝试使用 read.table
或 read.csv
从 *.txt
或 *.csv
文件中将数据读入 R。但是,我的数据在表示 1.4523*10^{-9}
的文件中写为 e.g. 1.4523e-9
,尽管 ggplot 将其识别为字符串而不是真实的。是否有某种 eval( )
函数可以将其转换为正确的值?
根据您导入的 csv
文件的确切格式,read.csv
和 read.table
通常只是将所有列转换为因子。由于直接转换为数字失败,我认为这是你的问题。您可以使用 colClasses
参数更改它:
# if every column should be numeric:
df <- read.csv("foobar.csv", colClasses = "numeric")
#if only some columns should be numeric, use a vector.
#to read the first as factor and the second as numeric:
read.csv("foobar.csv", colClasses = c("factor", "numeric")
当然,以上两个都是准系统示例;您可能还想提供其他参数,例如 header = T
.
如果您不想在阅读 table 时提供每列的 类(也许您还不知道它们!),您可以在事后使用以下任一项:
df$a <- as.numeric(as.character(a)) #as you already discovered
df$a <- as.numeric(levels(df$a)[df$a])
是的,它们都很笨重,但它们是标准的并且经常使用 recommended.
我正在尝试使用 read.table
或 read.csv
从 *.txt
或 *.csv
文件中将数据读入 R。但是,我的数据在表示 1.4523*10^{-9}
的文件中写为 e.g. 1.4523e-9
,尽管 ggplot 将其识别为字符串而不是真实的。是否有某种 eval( )
函数可以将其转换为正确的值?
根据您导入的 csv
文件的确切格式,read.csv
和 read.table
通常只是将所有列转换为因子。由于直接转换为数字失败,我认为这是你的问题。您可以使用 colClasses
参数更改它:
# if every column should be numeric:
df <- read.csv("foobar.csv", colClasses = "numeric")
#if only some columns should be numeric, use a vector.
#to read the first as factor and the second as numeric:
read.csv("foobar.csv", colClasses = c("factor", "numeric")
当然,以上两个都是准系统示例;您可能还想提供其他参数,例如 header = T
.
如果您不想在阅读 table 时提供每列的 类(也许您还不知道它们!),您可以在事后使用以下任一项:
df$a <- as.numeric(as.character(a)) #as you already discovered
df$a <- as.numeric(levels(df$a)[df$a])
是的,它们都很笨重,但它们是标准的并且经常使用 recommended.