r - R Dataframe Factor转换为数字问题
问题描述
我一直在尝试合并和排序几个 csv 文件(下面的链接)。我已成功合并文件,可以在 Excel 中手动对结果进行排序。但我想自动化这个并能够得到排序的结果。
问题 在最后一步中,我尝试在合并的 DF 中转换因子“rankingGDP”,以便能够按值按 desc 顺序对其进行排序。当我将结果 DF 分配给 order 函数时,每个国家的 GDP 排名值是完全不同的。数据已经错位。谁能告诉我我做错了什么。非常感谢
#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])
解决方案
因此,您正在尝试将因子转换为数字。举个例子:
> 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
有关更多信息,您可以访问 如何在不丢失信息的情况下将因子转换为整数\数字?
希望它会有所帮助。
推荐阅读
- asp.net-core - 为什么 Ctrl+C 不停止通用主机?
- c# - 从动态队列 RabbitMQ 读取数据
- python - 在python中使用selenium搜索电子邮件地址并在没有结果时返回信息失败
- docker - 主体反序列化失败(提供的身份无效:x509:证书已过期或尚未生效)
- ios - 在Objective C中添加二维数组的对角线
- python - 具有(单个)最大分区大小的星形和条形
- javascript - 道具更改时反应组件不重新渲染
- python-2.7 - P4Python 在删除客户端时遇到问题 - [错误]:要删除客户端,请先删除所有挂起的更改
- python - 如何将代码中的值与单独文本文档中的值进行比较?
- java - Map.merge .. 不能从静态上下文中引用非静态方法