将数据框列类型设置为字符而不是因子(默认)R

Set dataframe column type to character instead of factor (default) R

dataf <- data.frame(Alert=logical(),IQR=integer(),Kurtosis=integer(),Entropy=integer(),Skewness=integer(),Sex=character(),Complex=character(),Picos=integer(),PicosFil=integer(),Umbral=integer(),Gama=character(),stringsAsFactors=FALSE)
dataf <- rbind(dataf,list(Alert=FALSE,IQR=2.6938,Kurtosis=1.73447,Entropy=1.76160,Skewness=0.140613,Sex="Mujer",Complex="Slim",Picos=0,PicosFill=0,Umbral=15.708,Gama="Alta"))
dataf <- rbind(dataf,list(Alert=FALSE,IQR=0.179574,Kurtosis=19.0538,Entropy=0.74779,Skewness=1.1355,Sex="Mujer",Complex="Slim",Picos=1,PicosFill=1,Umbral=18.975,Gama="Media"))

我在使用 Gama 时遇到问题,当我输入新值(字符串)时,出现以下错误:

Warning message: In [<-.factor(*tmp*, ri, value = "Media") : invalid factor level, NA generated

您需要将 stringsAsFactors=FALSE 传递给您的列表。 你可以看到当你添加第一行时,你的 df 上的所有 character 列变成 factor

dataf <- data.frame(Alert=logical(),IQR=integer(),Kurtosis=integer(),Entropy=integer(),Skewness=integer(),Sex=character(),Complex=character(),Picos=integer(),PicosFil=integer(),Umbral=integer(),Gama=character(),stringsAsFactors=FALSE)
dataf <- rbind(dataf,list(Alert=FALSE,IQR=2.6938,Kurtosis=1.73447,Entropy=1.76160,Skewness=0.140613,Sex="Mujer",Complex="Slim",Picos=0,PicosFill=0,Umbral=15.708,Gama="Alta"), stringsAsFactors=FALSE)
dataf <- rbind(dataf,list(Alert=FALSE,IQR=0.179574,Kurtosis=19.0538,Entropy=0.74779,Skewness=1.1355,Sex="Mujer",Complex="Slim",Picos=1,PicosFill=1,Umbral=18.975,Gama="Media"), stringsAsFactors=FALSE)

您可以使用 do.call 来执行此操作。

lst <- list(
    list(Alert=FALSE,IQR=2.6938,Kurtosis=1.73447,Entropy=1.76160,Skewness=0.140613,Sex="Mujer",Complex="Slim",Picos=0,PicosFill=0,Umbral=15.708,Gama="Alta"),
    list(Alert=FALSE,IQR=0.179574,Kurtosis=19.0538,Entropy=0.74779,Skewness=1.1355,Sex="Mujer",Complex="Slim",Picos=1,PicosFill=1,Umbral=18.975,Gama="Media")
)
do.call(rbind.data.frame, lst)

注意do.call(rbind, lst)会return一个matrix,需要显式调用data.frame方法