楼主: dxystata
4587 17

[数据管理求助] levelsof 显示小数点后位数过长 [推广有奖]

版主

已卖:302份资源

大师

37%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
183395 个
通用积分
15333.1475
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
298627 点
帖子
5586
精华
1
在线时间
13632 小时
注册时间
2006-6-21
最后登录
2025-12-22

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

楼主
dxystata 发表于 2013-6-21 14:14:37 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
0621.rar (343 Bytes) 本附件包括:
  • 0621.dta


. levelsof pt1
10.60000038146973 11.60000038146973 11.80000019073486 12.19999980926514 14.39999961853027 14.69999980926514 15.19999980926514


希望得到的结果是
10.6 11.6 11.8 12.2 14.4 14.7 15.2
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Levels Level Leve EVE ELS 小数点

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h3327156 + 1 + 1 + 1 好问题!

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

本帖被以下文库推荐

沙发
sungmoo 发表于 2013-6-21 22:04:36
g double p=round(pt1,.1)
levelsof p
di "`r(levels)'"
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h3327156 + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

藤椅
jzhyue 发表于 2013-6-22 09:35:40
sungmoo 发表于 2013-6-21 22:04
g double p=round(pt1,.1)
levelsof p
di "`r(levels)'"
好像还是不行

. clear

. set obs 10
obs was 0, now 10

. gen pt1=10*uniform()

. g double p=round(pt1,.1)

. levelsof p
.5 .9 2 2.9 3.7 3.8 4.100000000000001 5 8.1

. di "`r(levels)'"
.5 .9 2 2.9 3.7 3.8 4.100000000000001 5 8.1

板凳
jzhyue 发表于 2013-6-22 09:37:43
*改成字符可以
g  p=string(round(pt1,.1))
levelsof p
di r(levels)
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h3327156 + 1 + 1 + 1 levelsof p,clean 会更清爽

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

报纸
sungmoo 发表于 2013-6-22 11:35:33
*改成字符可以
改成字符,要注意其中的引号

clear
set se 10000
set obs 10
g  p=string(round(10*uniform(),.1))

*注意以下的区别:
levelsof p,c
token r(levels)
n di `1'

levelsof p
token r(levels)
n di `1'

levelsof p,c
token `r(levels)'
n di `1'
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h3327156 + 1 + 1 + 1 学习了! tokenize 赞

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

地板
sungmoo 发表于 2013-6-22 12:37:12
好像还是不行
这个要看初始数据的精度。随机数生成与其他输入还有些区别。

help data types

Precision of numeric storage types

    floats have about 7 digits of accuracy; the magnitude of the number does not matter.  Thus,
    1234567 can be stored perfectly as a float, as can 1234567e+20.  The number 123456789, however,
    would be rounded to 123456792.  In general, this rounding does not matter.

    If you are storing identification numbers, the rounding could matter.  If the identification
    numbers are integers and take 9 digits or less, store them as longs; otherwise, store them as
    doubles.  doubles have 16 digits of accuracy.

    Stata stores numbers in binary, and this has a second effect on numbers less than 1.  1/10 has
    no perfect binary representation just as 1/11 has no perfect decimal representation.  In float,
    .1 is stored as .10000000149011612.  Note that there are 7 digits of accuracy, just as with
    numbers larger than 1.  Stata, however, performs all calculations in double precision.  If you
    were to store 0.1 in a float called x and then ask, say, "list if x==.1", there would be nothing
    in the list.  The .1 that you just typed was converted to double, with 16 digits of accuracy
    (.100000000000000014...), and that number is never equal to 0.1 stored with float accuracy.

    One solution is to type "list if x==float(.1)".  The float() function rounds its argument to
    float accuracy; see [D] functions.  The other alternative would be store your data as double,
    but this is probably a waste of memory.  Few people have data that is accurate to 1 part in 10
    to the 7th.  Among the exceptions are banks, who keep records accurate to the penny on amounts
    of billions of dollars.  If you are dealing with such financial data, store your dollar amounts
    as doubles.  See float().



******
clear all
set ty float 或 double       /*对应不同结果*/
inp x
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
end

levelsof x
n di "`r(levels)'"

***比较保险且省事的方法是,用group()将各值转换(对应)为整数,再利用整数操作(使用levelsof)。
已有 2 人评分学术水平 热心指数 信用等级 收起 理由
Sunknownay + 3 + 3 + 3 精彩帖子
h3327156 + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 4  热心指数 + 4  信用等级 + 4   查看全部评分

7
蓝色 发表于 2013-6-22 13:13:02
di  前面的 n 是什么的缩写啊
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h3327156 + 1 + 1 + 1 我也很想知道

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

8
dxystata 发表于 2013-6-22 15:31:40
蓝色 发表于 2013-6-22 13:13
di  前面的 n 是什么的缩写啊
noisily
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h3327156 + 1 + 1 + 1 感谢开示

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

9
dxystata 发表于 2013-6-22 16:41:21
tostring pt1,replace force usedisplayformat
destring pt1,replace
levelsof pt1

这是我的方法
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h3327156 + 1 + 1 + 1 两步后,其pt1的format与sungmoo的一步同

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

10
dxystata 发表于 2013-6-22 21:55:57
两步后,其pt1的format与sungmoo的一步同


如果变量很多,小数后有两位,有三位。效果就不一样啦!
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h3327156 + 1 + 1 + 1 学习,感谢

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-1 04:00