R数据帧因子转换为数字问题[重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我一直在尝试合并和排序几个csv文件(下面的链接)。我已经成功合并了文件,可以在excel中手动对结果进行排序。但是我想自动化这个并且能够得到排序结果。

问题在最后一步中,我尝试在合并的DF中转换因子'rankingGDP',以便能够按值按顺序对它进行排序。当我将结果DF分配给订单函数时,每个国家/地区的排名GSD值完全不同。数据变得不对齐。任何人都可以告诉我我做错了什么。谢谢堆

   #Fetch the files
    fileUrl <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FGDP.csv"
    download.file(fileUrl, destfile="./fgdp.csv")
    fileUrl <-"https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FEDSTATS_Country.csv"
    download.file(fileUrl, destfile="./fed.csv")

    #Read the files
    fgdp <- read.csv("fgdp.csv",skip = 4, header = T)
    fed <- read.csv("fed.csv" ,header = T)

    #subset relevant columns
    fgdp <- fgdp[,c(1,2,4,5)]

    #remove rows that are empty
    fed <- fed[rowSums(is.na(fed))<ncol(fed),]
    fgdp <- fgdp[rowSums(is.na(fgdp))<ncol(fgdp),]

    #name the columns for fgdp to match fed
    colnames(fgdp) <- c("CountryCode","rankingGDP", 
                        "Long.Name", "gdp")

    #merge the files based on Country Code
    dt <- merge(fgdp, fed, by.x ="CountryCode", by.y = "CountryCode", all = TRUE)

    #Remove  rows where the relevant columns are empty
    dt <- dt[!dt$CountryCode=="" ,]
    dt <- dt[!(dt$rankingGDP=="" | is.na(dt$rankingGDP)) ,]

    #subset the columns used for analysis
    dt1 <- dt[,1:4]

    #remove NAs
    dt1 <- dt1[!(is.na(dt1$rankingGDP)),]

    #Convert factor to numeric to be able to sort rankingGDP decending
    #THE ISSUE IS HERE WHERE THE result gives me different values for the
    #rankingGDP column(2). By that I mean factor numbers(type chars) are not
    #converted to the associated number in most cases.

    dt1[,2]<- as.numeric(dt1[,2])
r type-conversion data-conversion
2个回答
0
投票

我通过将stringsAsFactors = F添加到3个位置重新运行您的脚本,它现在似乎正常工作:

fgdp <- read.csv("fgdp.csv",skip = 4, header = T, stringsAsFactors=F)
fed <- read.csv("fed.csv" ,header = T, stringsAsFactors=F)

dt <- merge(fgdp, fed, by.x ="CountryCode", by.y = "CountryCode", all = TRUE, stringsAsFactors=F)

让我知道它是否适合你


0
投票

所以你试图将因子转换为数字。我们来举个例子:

> x <- factor(rep(11:20,2))
> x
[1] 11 12 13 14 15 16 17 18 19 20 11 12 13 14 15 16 17 18 19 20
Levels: 11 12 13 14 15 16 17 18 19 20

如果您现在尝试将其转换为数字。然后它会给你以下结果。

> as.numeric(x)
[1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10

它不会返回所需的输出。为此,您需要执行以下操作:

> as.numeric(levels(x))[x]
[1] 11 12 13 14 15 16 17 18 19 20 11 12 13 14 15 16 17 18 19 20

您可以将其应用于您的数据。

>as.numeric(levels(dt1[,2]))[dt1[,2]]
[1] 161 105  60 125  32  26 133 172  12  27  68 162  25 140 128  59  76  93
[19] 138 111  69 169 149  96   7 153 113 167 117 165  11  20  36   2  99  98
[37] 121  30 182 166  81  67 102  51   4 183  33  72  48  64  38 159  13 103
[55]  85  43 155   5 185 109   6 114  86 148 175 176 110  42 178  77 160  37
[73] 108  71 139  58  16  10  46  22  47 122  40   9 116  92   3  50  87 145
[91] 120 189 178  15 146  56 136  83 168 171  70 163  84  74  94  82  62 147
[109] 141 132 164  14 188 135 129 137 151 130 118 154 127 152  34 123 144  39
[127] 126  18  23 107  55  66  44  89  49  41 187 115  24  61  45  97  54  52
[145]   8 142  19  73 119  35 174 157 100  88 131 186 150  63  80  21 158 173
[163]  65 124 156  31 143  91 170 184 101  79  17 190  95 106  53  78   1  75
[181] 180  29  57 177 181  90  28 112 104 134

有关更多信息,您可以访问How to convert a factor to integer\numeric without loss of information?

希望它会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.