无法将类型字符转换为R中的数字

时间:2016-04-30 06:56:48

标签: r

我的数据框中的列存储为字符。当我使用聚合函数时因为对字符使用操作而出错。我可以找到一个类似的问题,但提供的解决方案,即as.numeric(as.character(dataframe))对我不起作用。如何更改列的类型并保留列的值?

> str(df$Order_Quantity)
  chr [1:92] "1,909.60" "2,019.20" "4,119.60" "-" "11,000.00" "1110.00"  "80.5" ...
> df$Order_Quantity<- as.numeric(as.character(df$Order_Quantity))
 Warning message:
 NAs introduced by coercion 
> str(df$Order_Quantity)
 num [1:92] NA NA NA NA NA NA 10.5 NA NA 26 ...
#I am losing my data 

1 个答案:

答案 0 :(得分:1)

使用[PostBuildScript] - Execution post build scripts. [PostBuildScript] - Resolving environment variables for the script content. [PostBuildScript] - Evaluating the script: /var/lib/jenkins/sputnik/bin/sputnik --conf /var/lib/jenkins/sputnik/gerrit.properties --changeId $GERRIT_CHANGE_ID --revisionId $GERRIT_PATCHSET_REVISION echo "exit 0 workaround" [project1] $ /bin/sh -xe /tmp/hudson6990025050616459512.sh + /var/lib/jenkins/sputnik/bin/sputnik --conf /var/lib/jenkins/sputnik/gerrit.properties --changeId --revisionId Sputnik version 1.7.0 Sputnik - review your Gerrit patchset with Checkstyle, PMD and FindBugs usage: sputnik [--apiKey <apiKey>] [--buildId <buildId>] [--changeId <changeId>] --conf <conf> [--pullRequestId <pullRequestId>] [--revisionId <revisionId>] --apiKey <apiKey> Optional API key for using Sputnik for Github --buildId <buildId> Optional build id for using Sputnik for Github --changeId <changeId> Gerrit change id --conf <conf> Configuration properties file --pullRequestId <pullRequestId> Stash pull request id --revisionId <revisionId> Gerrit revision id Missing argument for option: changeId Build step 'Execute a set of scripts' changed build result to FAILURE Build step 'Execute a set of scripts' marked build as failure Finished: FAILURE 删除逗号:

gsub()

我还注意到您的源数据似乎也有短划线(df$Order_Quantity<- as.numeric(gsub(",", "", as.character(df$Order_Quantity))) )。你可以忽略这一点,它只会被-取代,或者你可以按照你的需要处理它。