使用 R 将字符变量转换为数字

Convert character variables into numeric with R

第一个问题 ;)

我有这个数据集,并且我在 read.csv 函数中没有使用“stringsAsFactors=FALSE”。我无法处理这些数据,因为我收到了警告消息:强制引入了 NA。感谢您的帮助:)

rm(list=ls())

path <- "....."
file <- read.csv(path, header = TRUE, sep = ",", stringsAsFactors=FALSE)

str(file)
#'data.frame':  33 obs. of  11 variables:
#$ Var1: chr  "01/09/2021" "02/09/2021" "09/09/2021" "10/09/2021" ...
#$ Var2: chr  "mercoledì" "giovedì" "giovedì" "venerdì" ...
#$ Var3: chr  "2,5" "2,5" "2,5" "3,0" ...
#$ Var4: chr  "4,0" "0,0" "2,0" "3,0" ...
#$ Var5: chr  "2,0" "5,0" "5,0" "5,0" ...
#$ Var5: chr  "0,0" "0,0" "0,0" "0,0" ...
#$ Var6: chr  "6,0" "5,0" "7,0" "8,0" ...
#$ Var7: chr  "23,5" "25,0" "28,0" "32,0" ...
#$ Var8: chr  "0,0" "1,0" "5,0" "5,5" ...
#$ Var9: chr  "23,5" "26,0" "33,0" "37,5" ...
#$ Var10: chr  "67,0" "0,0" "0,0" "0,0" ...

as.numeric(file$Var7)

1 呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐呐 警告信息: 强制引入的 NAs

CSV FILE

我成功重现了你的问题。您的文件使用 , 作为字段分隔符和小数点分隔符(这并不常见)。

您可以通过在read.csv()中指定(dec = ",")中的小数点为逗号来解决您的问题,如下所示:

read.csv(
  path, 
  header = TRUE, 
  sep = ",", 
  dec = ",", # I've added this line
  stringsAsFactors = FALSE
)

再次更改此设置,运行 str(file),您应该会看到大多数列都是数字。