tostring 将字符值转换为整数
tostring turns character values into integers
我正在尝试转换下图中 Stata (AP1) 中的一列,以便条目为字符串类型。当前条目显示为字符(例如“cultivateur”),但显示为 int
类型。
我使用以下代码尝试将它们更改为字符串。
label values AP1 .
tostring AP1, replace
AP1 was long now str5
虽然 AP1
列变为字符串类型,但所有字符现在都变为整数,这不是我根据观察对数据进行子集化所需要的。
有谁知道我可以将此列切换为字符串类型而不用将字符变为整数吗?
您的图像在 phone 或膝上型电脑上几乎不可读,但使用非常大的显示器的任何人都可以阅读。请参阅 Stata 标签 wiki 以获取有关呈现可重现数据示例的指南。
正在发生的事情可以通过考虑
来重复解释
. sysuse auto, clear
(1978 automobile data)
. tab foreign
Car origin | Freq. Percent Cum.
------------+-----------------------------------
Domestic | 52 70.27 70.27
Foreign | 22 29.73 100.00
------------+-----------------------------------
Total | 74 100.00
. tab foreign, nolabel
Car origin | Freq. Percent Cum.
------------+-----------------------------------
0 | 52 70.27 70.27
1 | 22 29.73 100.00
------------+-----------------------------------
Total | 74 100.00
. tostring foreign, gen(str_foreign)
str_foreign generated as str1
. tab str_foreign
Car origin | Freq. Percent Cum.
------------+-----------------------------------
0 | 52 70.27 70.27
1 | 22 29.73 100.00
------------+-----------------------------------
Total | 74 100.00
. d *foreign
Variable Storage Display Value
name type format label Variable label
------------------------------------------------------------------------------------------------------------------
foreign byte %8.0g origin Car origin
str_foreign str1 %9s Car origin
foreign
就像你的问题变量是一个带有值标签的数字变量。 (术语“列”在 Stata 中对于数据集中的变量不是标准的。)通过 tostring
推它,你会得到一个包含整数字符的字符串变量。 Stata 完成了您的要求。
要获取包含值标签文本的字符串变量,您需要应用 decode
命令,该命令正是为此目的而编写的(顺便说一下,long predates tostring
作为一个官方命令)。
我正在尝试转换下图中 Stata (AP1) 中的一列,以便条目为字符串类型。当前条目显示为字符(例如“cultivateur”),但显示为 int
类型。
我使用以下代码尝试将它们更改为字符串。
label values AP1 .
tostring AP1, replace
AP1 was long now str5
虽然 AP1
列变为字符串类型,但所有字符现在都变为整数,这不是我根据观察对数据进行子集化所需要的。
有谁知道我可以将此列切换为字符串类型而不用将字符变为整数吗?
您的图像在 phone 或膝上型电脑上几乎不可读,但使用非常大的显示器的任何人都可以阅读。请参阅 Stata 标签 wiki 以获取有关呈现可重现数据示例的指南。
正在发生的事情可以通过考虑
来重复解释. sysuse auto, clear
(1978 automobile data)
. tab foreign
Car origin | Freq. Percent Cum.
------------+-----------------------------------
Domestic | 52 70.27 70.27
Foreign | 22 29.73 100.00
------------+-----------------------------------
Total | 74 100.00
. tab foreign, nolabel
Car origin | Freq. Percent Cum.
------------+-----------------------------------
0 | 52 70.27 70.27
1 | 22 29.73 100.00
------------+-----------------------------------
Total | 74 100.00
. tostring foreign, gen(str_foreign)
str_foreign generated as str1
. tab str_foreign
Car origin | Freq. Percent Cum.
------------+-----------------------------------
0 | 52 70.27 70.27
1 | 22 29.73 100.00
------------+-----------------------------------
Total | 74 100.00
. d *foreign
Variable Storage Display Value
name type format label Variable label
------------------------------------------------------------------------------------------------------------------
foreign byte %8.0g origin Car origin
str_foreign str1 %9s Car origin
foreign
就像你的问题变量是一个带有值标签的数字变量。 (术语“列”在 Stata 中对于数据集中的变量不是标准的。)通过 tostring
推它,你会得到一个包含整数字符的字符串变量。 Stata 完成了您的要求。
要获取包含值标签文本的字符串变量,您需要应用 decode
命令,该命令正是为此目的而编写的(顺便说一下,long predates tostring
作为一个官方命令)。