R中L的澄清 [英] Clarification of L in R

查看:14
本文介绍了R中L的澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中的 L 的踪迹是:

c<-1:10C# [1] 1 2 3 4 5 6 7 8 9 10c[-1]# [1] 2 3 4 5 6 7 8 9 10c[-2]# [1] 1 3 4 5 6 7 8 9 10c[-1L]# [1] 2 3 4 5 6 7 8 9 10c[-2L]# [1] 1 3 4 5 6 7 8 9 10

我尝试使用 ?L 没有成功.

x[<n>L] 究竟是什么?有没有进一步使用的例子?

解决方案

这个答案是上面评论的总结.它基本上只是指向各种帮助文本的指针,但从 OP 对 ?L 的尝试可以看出,要找到相关的帮助文本并不总是那么容易.我期待在 ?as.integer 中找到有关 L 的内容,但没有.希望这个答案比一堆评论更有用.

  • R 语言中定义你会发现:我们可以使用 L 后缀来限定任何数字意图使其成为一个明确的整数"

  • From ?NumericConstants: "[...] 所有其他数字常量开始带有数字或句点,并且是十进制或十六进制常量可选地后跟 L"

    "紧跟 L 的数字常量被视为可能时的整数(如果它包含.")."

    "你可以结合"0x"以L"为前缀后缀".

  • 您可能还会发现检查浮点示例很有用与重访的两种"部分中的整数对比;这里."将大写L(如long")放在一个数字后,使R创建它为一个整数".

  • 不是专门关于 L,但总是与浮点相关与整数上下文是 FAQ7.31:为什么 R 认为这些数字不相等?".


讨论L效率的线程:

R-help 上的线程,其他人一直在努力寻找有关 L 的文档,并可能解释了为什么字母 L 以及为什么是 L vs as.integer 在效率方面.

  1. 10L和10L的区别

第一威廉·邓拉普:

<块引用>

为什么不使用 10I 表示整数?也许是因为I"和l"看起来太相似了,可能是因为i"和"听起来太相似了.L"并不意味着长":整数是 4 个字节长.

然后是布赖恩·里普利:

<块引用>

实际上是这样:这个符号可以追溯到 16 位的 C 语言整数为 16 位,长整数为 32 位的计算机(而 R 有没有长"类型).

<块引用>

R 中 this 的作者从来没有解释过他为什么选择这个符号,但是它比 as.integer(10) 短,并且 因为强制转换的效率更高在解析时完成.

  1. L 字
    讨论不同情况下的效率,并进行一些基准测试.

  2. R 历史:为什么 '大号;在整数常量的后缀字符L"中?

  3. 更多讨论在这里.

My trail on L in R is:

c<-1:10
c
# [1]  1  2  3  4  5  6  7  8  9 10
c[-1]
# [1]  2  3  4  5  6  7  8  9 10
c[-2]
# [1]  1  3  4  5  6  7  8  9 10
c[-1L]
# [1]  2  3  4  5  6  7  8  9 10
c[-2L]
# [1]  1  3  4  5  6  7  8  9 10

I tried using ?L without success.

What indeed is x[<n>L]? Any example for further usage of it?

解决方案

This answer is a summary of the comments above. It is basically just pointers to various help texts, but as evident from OP's attempt with ?L, it is not always easy to find the relevant help text. I was expecting to find something about L in ?as.integer, but no. Hopefully this answer is more useful than a pile of comments.

  • In the R language definition you will find: "We can use the L suffix to qualify any number with the intent of making it an explicit integer"

  • From ?NumericConstants: "[...] All other numeric constants start with a digit or period and are either a decimal or hexadecimal constant optionally followed by L"

    "An numeric constant immediately followed by L is regarded as an integer number when possible (and with a warning if it contains a ".")."

    "You can combine the "0x" prefix with the "L" suffix".

  • You may also find it useful to check the examples on floating point vs. integers in the section "Two Kinds Revisited" here. "Put capital L (as in "long") after a number to make R create it as an integer".

  • Not specifically about L, but always relevant in the floating point vs. integers context is FAQ7.31: "Why doesn’t R think these numbers are equal?".


Threads with discussions about the efficiency of L:

Threads on R-help where others have struggled to find documentation about L, with a possible explanation of why the letter L, and why L vs as.integer in terms of efficiency.

  1. Difference between 10 and 10L

First William Dunlap:

Why not 10I for integer? Perhaps because "I" and "l" look too similar, perhaps because "i" and "I" sound too similar. The "L" does not mean "long": integers are 4 bytes long.

Then Brian Ripley:

Actually it does: this notation dates from the C language on 16-bit computers where integers were 16-bits and longs were 32-bit (and R has no 'long' type).

The author of this in R never explained why he chose the notation, but it is shorter than as.integer(10), and more efficient as the coercion is done at parse time.

  1. The L Word
    Discussion about the efficiency in different situations, with some benchmarkings.

  2. R history: Why 'L; in suffix character ‘L’ for integer constants?

  3. More discussions here.

这篇关于R中L的澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆