如何使列表吗? - How to make lists?

- 此内容更新于:2016-02-24
主题:

我如何写一个函数,它接受两个参数:一个向量=V=X,并返回两个列表:首先是一个向量包含V的数字低于或等于X,第二个是一个向量的数字高于X向量应该不包括重复数字(如果一个数字重复它不应该出现不止一次)

原文:

How can I write a function that receives 2 arguments: a vector=V and a number=X, and returns two lists: First is a vector which contains all the digits in V that are below or equal to X, Second is a vector of digits that are above X. The vectors should not include repeated digits (if a digit repeats itself it should not appear more than once)

网友:请尝试解决问题寻求帮助之前你所拥有的。如果您计划使用编程在未来你应该读这个优秀的页面:www.whathaveyoutried.com。

(原文:Please attempt to solve the problem on you own before asking for help. If you plan to work with programming in the future you should read this excellent page: www.whathaveyoutried.com.)

网友:R函数只能返回一件事。你可以返回一个包含两个这听起来像它会接受你。

(原文:An R function can only return one thing. You can return a list which contains two vectors which sounds like it would be acceptable to you. return(list(belowx, abovex)))

解决方案:
周五免费赠品…
原文:

Freebie Friday...

fun <- function(x, v)
{
  z <- unique(v)
  list('<=' = z[z<=x], '>' = z[z>x])
}

> set.seed(152)
> X <- 5
> V <- sample(15, 100, TRUE)
> fun(X, V)
# $`<=`
# [1] 4 2 1 3 5
# 
# $`>`
# [1] 15 12 10  9 11  8  7 14 13  6