楼主: peijianshi
2010 3

[程序分享] for循环中出现的L表示什么意思? [推广有奖]

  • 0关注
  • 16粉丝

已卖:352份资源

副教授

80%

还不是VIP/贵宾

-

威望
0
论坛币
638 个
通用积分
2.3662
学术水平
12 点
热心指数
12 点
信用等级
5 点
经验
15373 点
帖子
636
精华
0
在线时间
568 小时
注册时间
2010-3-11
最后登录
2022-9-8

楼主
peijianshi 发表于 2017-4-12 21:42:02 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
在读别人函数时, 发现for循环中数值1后边有个L,

for(i in 1L : P) {略}


此处L表示什么意思?干什么用的?
二维码

扫码加我 拉你入群

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

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

关键词:for循环 For

R万岁!

沙发
ntsean 发表于 2017-4-12 21:56:48
integer

藤椅
peijianshi 发表于 2017-4-13 17:52:26
ntsean 发表于 2017-4-12 21:56
integer
还能再具体一点吗?
1:p本身就可以表示从1到p的整数吗?

板凳
peijianshi 发表于 2017-4-13 18:21:21
我在网络上找到答案了。

So, @James and @Brian explained what 3L means. But why would you use it?

Most of the time it makes no difference - but sometimes you can use it to get your code to run faster and consume less memory. A double ("numeric") vector uses 8 bytes per element. An integer vector uses only 4 bytes per element. For large vectors, that's less wasted memory and less to wade through for the CPU (so it's typically faster).

Mostly this applies when working with indices. Here's an example where adding 1 to an integer vector turns it into a double vector:
x <- 1:100
typeof(x) # integer

y <- x+1
typeof(y) # double, twice the memory size
object.size(y) # 840 bytes (on win64)

z <- x+1L
typeof(z) # still integer
object.size(z) # 440 bytes (on win64)

...but also note that working excessively with integers can be dangerous:
1e9L * 2L # Works fine; fast lean and mean!
1e9L * 4L # Ooops, overflow!

...and as @Gavin pointed out, the range for integers is roughly -2e9 to 2e9.

A caveat though is that this applies to the current R version (2.13). R might change this at some point (64-bit integers would be sweet, which could enable vectors of length > 2e9). To be safe, you should use .Machine$integer.max whenever you need the maximum integer value (and negate that for the minimum).

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 01:11