将 integer64 转换为 R 中的整数
Convert integer64 into integer in R
我正在编写几周前开始的代码。但是,(我不确定为什么)代码突然将一些列转换为 integer64 格式。
使用这种格式,其余代码不再 运行 并且我不熟悉 integer64 格式。我只想将 integer64 列转换为整数或数字格式。通常的命令 as.numeric 或 as.integer 不起作用。我能做什么?
这是我的数据样本
> dput(head(my_data3, n = 30))
structure(list(year = c(2007, 2009, 2011, 2012, 2005, 2017, 2001,
2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012, 2013, 2014, 2016,
2017, 2018, 2018, 2011, 2014, 2002, 2015, 2004, 2008, 2009, 2011,
2012), export_value = structure(c(0, 0, 0, 0, 5.74548939548786e-319,
3.29556607745487e-319, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.20704064653743e-319,
0, 0, 0, 3.13731685109192e-320, 3.82307996751957e-320, 0, 0,
1.39406056696212e-318, 7.3185450052818e-319, 1.72873569479852e-320,
7.06760906375903e-320, 1.04667807071468e-319, 2.30382810655773e-320,
1.01915861424132e-319), class = "integer64"), location_code = c("AGO",
"AGO", "AGO", "AGO", "MDG", "MDG", "MOZ", "MOZ", "MOZ", "MOZ",
"MOZ", "MOZ", "MOZ", "MOZ", "MOZ", "MOZ", "MOZ", "MOZ", "MOZ",
"MOZ", "SWZ", "SYC", "SYC", "TZA", "TZA", "ZAF", "ZAF", "ZAF",
"ZAF", "ZAF"), partner_code = c("AFG", "AFG", "AFG", "AFG", "AFG",
"AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG",
"AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG",
"AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG"), export_destination = c("RoW",
"RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW",
"RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW",
"RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW",
"RoW", "RoW"), product_group = c("Food", "Food", "Food", "Food",
"Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food",
"Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food",
"Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food",
"Food", "Food"), exporter = c("Angola", "Angola", "Angola", "Angola",
"Madagascar", "Madagascar", "Mozambique", "Mozambique", "Mozambique",
"Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique",
"Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique",
"Mozambique", "Eswatini", "Seychelles", "Seychelles", "Tanzania",
"Tanzania", "South Africa", "South Africa", "South Africa", "South Africa",
"South Africa"), export_destination_country = c("Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan")), row.names = c(NA,
-30L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000295b7981ef0>)
integer64 class 是由 bit64 包创建的。有many questions that have arisen over the years that have been answered on SO个。需要通过使用创建 integer64 对象的包中的函数来恢复数据而不进行破坏。
library(bit64)
?integer64
# You might imagine that as.numeric should have an integer64 method.
# .... but like me you would have been wrong
#Instead, division is defined for integer64 objects and it returns a double.
# .... so divide by 1 ( if and only if you have installed and loaded pkg:bit64
my_data3$export_value/1
[1] 0 0 0 0 116290 66703 0 0 0 0 0 0 0 0 0 44671
[17] 0 0 0 6350 7738 0 0 282161 148129 3499 14305 21185 4663 20628
我之前没有注意到为 'integer64' 对象同时定义了 as.integer
和 as.double
泛型,所以使用它们可能会更好。当然,如果需要整数,以 as.integer
.
开头可能会更好
我想这些早期问题和答案中的一个或多个可能包含对这个问题的答案,但我在查看的前 5 个中没有遇到重复的问题。
我正在编写几周前开始的代码。但是,(我不确定为什么)代码突然将一些列转换为 integer64 格式。 使用这种格式,其余代码不再 运行 并且我不熟悉 integer64 格式。我只想将 integer64 列转换为整数或数字格式。通常的命令 as.numeric 或 as.integer 不起作用。我能做什么?
这是我的数据样本
> dput(head(my_data3, n = 30))
structure(list(year = c(2007, 2009, 2011, 2012, 2005, 2017, 2001,
2004, 2005, 2006, 2007, 2008, 2010, 2011, 2012, 2013, 2014, 2016,
2017, 2018, 2018, 2011, 2014, 2002, 2015, 2004, 2008, 2009, 2011,
2012), export_value = structure(c(0, 0, 0, 0, 5.74548939548786e-319,
3.29556607745487e-319, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.20704064653743e-319,
0, 0, 0, 3.13731685109192e-320, 3.82307996751957e-320, 0, 0,
1.39406056696212e-318, 7.3185450052818e-319, 1.72873569479852e-320,
7.06760906375903e-320, 1.04667807071468e-319, 2.30382810655773e-320,
1.01915861424132e-319), class = "integer64"), location_code = c("AGO",
"AGO", "AGO", "AGO", "MDG", "MDG", "MOZ", "MOZ", "MOZ", "MOZ",
"MOZ", "MOZ", "MOZ", "MOZ", "MOZ", "MOZ", "MOZ", "MOZ", "MOZ",
"MOZ", "SWZ", "SYC", "SYC", "TZA", "TZA", "ZAF", "ZAF", "ZAF",
"ZAF", "ZAF"), partner_code = c("AFG", "AFG", "AFG", "AFG", "AFG",
"AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG",
"AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG",
"AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG"), export_destination = c("RoW",
"RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW",
"RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW",
"RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW", "RoW",
"RoW", "RoW"), product_group = c("Food", "Food", "Food", "Food",
"Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food",
"Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food",
"Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food",
"Food", "Food"), exporter = c("Angola", "Angola", "Angola", "Angola",
"Madagascar", "Madagascar", "Mozambique", "Mozambique", "Mozambique",
"Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique",
"Mozambique", "Mozambique", "Mozambique", "Mozambique", "Mozambique",
"Mozambique", "Eswatini", "Seychelles", "Seychelles", "Tanzania",
"Tanzania", "South Africa", "South Africa", "South Africa", "South Africa",
"South Africa"), export_destination_country = c("Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan")), row.names = c(NA,
-30L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000295b7981ef0>)
integer64 class 是由 bit64 包创建的。有many questions that have arisen over the years that have been answered on SO个。需要通过使用创建 integer64 对象的包中的函数来恢复数据而不进行破坏。
library(bit64)
?integer64
# You might imagine that as.numeric should have an integer64 method.
# .... but like me you would have been wrong
#Instead, division is defined for integer64 objects and it returns a double.
# .... so divide by 1 ( if and only if you have installed and loaded pkg:bit64
my_data3$export_value/1
[1] 0 0 0 0 116290 66703 0 0 0 0 0 0 0 0 0 44671
[17] 0 0 0 6350 7738 0 0 282161 148129 3499 14305 21185 4663 20628
我之前没有注意到为 'integer64' 对象同时定义了 as.integer
和 as.double
泛型,所以使用它们可能会更好。当然,如果需要整数,以 as.integer
.
我想这些早期问题和答案中的一个或多个可能包含对这个问题的答案,但我在查看的前 5 个中没有遇到重复的问题。