R Dataframe因子转换为数字问题
R Dataframe Factor conversion to numeric issue
我一直在尝试合并和排序几个 csv 文件(下面的链接)。
我已成功合并文件并可以在 excel 中手动对结果进行排序。但我想将其自动化并能够得到排序的结果。
问题
在最后一步中,我尝试将合并后的 DF 中的因子 'rankingGDP' 转换为能够按值按 desc 顺序对其进行排序。
当我将结果 DF 分配给排序函数时,每个国家/地区的 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
它不会return 您想要的输出。为此,您需要执行以下操作:
> 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?
希望对您有所帮助。
我通过将 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)
让我知道它是否适合你
我一直在尝试合并和排序几个 csv 文件(下面的链接)。 我已成功合并文件并可以在 excel 中手动对结果进行排序。但我想将其自动化并能够得到排序的结果。
问题 在最后一步中,我尝试将合并后的 DF 中的因子 'rankingGDP' 转换为能够按值按 desc 顺序对其进行排序。 当我将结果 DF 分配给排序函数时,每个国家/地区的 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
它不会return 您想要的输出。为此,您需要执行以下操作:
> 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?
希望对您有所帮助。
我通过将 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)
让我知道它是否适合你