仍然试图进入R逻辑...解压缩(在LHS上)返回多个值的函数的结果的"最佳"方法是什么?
我显然不能这样做:
R> functionReturningTwoValues <- function() { return(c(1, 2)) }
R> functionReturningTwoValues()
[1] 1 2
R> a, b <- functionReturningTwoValues()
Error: unexpected ',' in "a,"
R> c(a, b) <- functionReturningTwoValues()
Error in c(a, b) <- functionReturningTwoValues() : object 'a' not found
Run Code Online (Sandbox Code Playgroud)
我真的必须做以下事吗?
R> r <- functionReturningTwoValues()
R> a <- r[1]; b <- r[2]
Run Code Online (Sandbox Code Playgroud)
或者R程序员会写更像这样的东西:
R> functionReturningTwoValues <- function() {return(list(first=1, second=2))}
R> r <- functionReturningTwoValues()
R> r$first
[1] 1
R> r$second
[1] 2
Run Code Online (Sandbox Code Playgroud)
---编辑回答谢恩的问题---
我真的不需要给结果值部分命名.我申请一个聚合函数的第一个组件和其他到第二组件(min
和max
,如果它是我不需要分裂他们两个组件功能相同).
Lua函数可以返回多个结果:
a, b, c = unpack({'one', 'two', 'three'})
Run Code Online (Sandbox Code Playgroud)
如果我对第三个返回值不感兴趣,我可以选择在调用函数时忽略它:
a, b = unpack({'one', 'two', 'three'})
Run Code Online (Sandbox Code Playgroud)
在调用函数时是否有类似的方法忽略X的第一个元素?
如果我只想要第三个返回值,我可以编写这段代码,但我想知道是否存在更清晰的代码:
_, _, c = unpack({'one', 'two', 'three'})
Run Code Online (Sandbox Code Playgroud) 嗨我有一个包含许多表和这样的外键的数据库
CREATE TABLE IF NOT EXISTS `articulos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nombre` varchar(63) NOT NULL,
`contenido` text NOT NULL,
`normas_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=138 ;
CREATE TABLE IF NOT EXISTS `aspectosambientales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nombre` varchar(63) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ;
CREATE TABLE IF NOT EXISTS `aspectosambientales_articulos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`aspectosambientales_id` int(11) NOT NULL,
`articulos_id` int(11) NOT NULL,
PRIMARY …
Run Code Online (Sandbox Code Playgroud)