I would like to take a random sample of rows from a data.frame, apply a function to the subset, then take a sample from the remaining rows, apply the function to the new subset (with different parameters), and so on. A simple example would be if 5% of a population dies each month, in month 2 I need the population minus those ones who died in time month 1. I have put together a very verbose me
我想从data.frame中随机取样行,将函数应用于子集,然后从其余行中取样,将函数应用于新子集(使用不同参数)等等。 一个简单的例子是,如果每个月有5%的人死亡,第二个月我需要的人数减去那些在第一个月死亡的人。 我已经制定了一个非常详细的方法,涉及从抽样行中保存ID,然后将它们从第二阶段的数据中分类出来等等。 library(data.table) dt <- data.table(Number=1:100, ID=paste0("A", 1:100)) first<-dt[samp
I want to use subset within another function but to pass on the non-standard evaluation arguments from the top-level function. The following is non-working code, but outlines the idea: foo_1 <- function(x, mysubset) { # some deparse, substitute etc. magic here ?? subset(x, subset) } foo_1(ansombe, x1 > 5) I want this to get the same results as for subset(ansombe, x1 > 5) . Also,
我想在另一个函数中使用subset ,但是要从顶层函数传递非标准的评估参数。 以下是非工作代码,但概述了这个想法: foo_1 <- function(x, mysubset) { # some deparse, substitute etc. magic here ?? subset(x, subset) } foo_1(ansombe, x1 > 5) 我希望得到与subset(ansombe, x1 > 5)相同的结果。 另外,我希望当参数传递到更深层次时也是如此 foo_2 <- function(x, mysubset) { # some deparse, sub
I am a student taking R. I have directory with lot's of files inside of it. i need to write a function named 'pollutantmean' to calculate the mean of a pollutant (either sulfate or nitrate) from a data set (see example below) the function takes three arguments: 'directory', 'pollutant', and 'id'. as a part of my function, I have successfully read all the fil
我是一名参加R的学生。我有很多文件夹在里面。 我需要编写一个名为'pollutantmean'的函数来计算污染物(硫酸盐或硝酸盐)的平均值(参见下面的例子),该函数有三个参数:'目录','污染物'和'id' 。 作为函数的一部分,我已经成功读取所有文件并将它们放入单个文件中,以便现在可以执行一些计算,如中位数,平均值等。我已经使用rbind和for循环来创建data.frame。 但问题是,在创建数据框
Below I have provided a snippet of some code that I have been working on. I have been sucessfully reading in string as tables. There exist some subset of my tables which I wish to use the median() function on. From what research I have done and my own experiences median() does not have defined behavior for a data.frame nor a subset of data.frame. So, wishing to fit my problem into some define
下面我提供了一些我一直在研究的代码片段。 我已经成功地以字符串形式读取表格。 有一些我希望使用median()函数的表的子集。 根据我所做的研究和我自己的经验,median()没有为data.frame定义行为,也没有data.frame的子集。 所以,为了让我的问题适合某些定义的行为,我试图将我想要的子集投射到一个向量中。 但是,即使在使用as.vector投射我想要的子集之后,我仍然有一个data.frame。 当我尝试对此调用中位数时,我
The following code is obviously wrong. What's the problem? i <- 0.1 i <- i + 0.05 i ## [1] 0.15 if(i==0.15) cat("i equals 0.15") else cat("i does not equal 0.15") ## i does not equal 0.15 General (language agnostic) reason Since not all numbers can be represented exactly in IEEE floating point arithmetic (the standard that almost all computers use to represent decimal numbers and d
下面的代码显然是错误的。 有什么问题? i <- 0.1 i <- i + 0.05 i ## [1] 0.15 if(i==0.15) cat("i equals 0.15") else cat("i does not equal 0.15") ## i does not equal 0.15 一般(语言不可知)原因 由于不是所有的数字都可以用IEEE浮点算法(几乎所有计算机用来表示十进制数并与它们进行数学运算的标准)精确表示,所以您不会总能得到您所期望的。 这是特别真实的,因为一些简单的有限小数(例如0.1和0.05)的
In R what is the difference between a library and a package ? I have come across posts where people refer to packages within a library. Based on this idea I interpret it that a package lives in a library (ie I store my packages with a designated library). However I get confused when I want to use package 'x' . I am under the imperssion I need to call the library function to get pack
在R 库和包之间有什么区别? 我遇到过有人在图书馆中提到软件包的帖子。 基于这个想法,我解释了一个包裹住在一个图书馆里(即我用指定的图书馆储存我的包裹)。 然而,当我想使用包'x'时,我感到困惑。 我在我需要调用库函数来获取软件包'x'被使用的imperssion下? 一旦我已经有在包称为“X”包“x”的功能则成为提供给我吗? 在R中,包是R函数,数据和编译代码的集合。 存储包的位置称为库。 如果您需
I often seen the symbol 1L (or 2L , 3L , etc) appear in R code. Whats the difference between 1L and 1 ? 1==1L evaluates to TRUE . Why is 1L used in R code? 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") vec
我经常看到符号1L (或2L , 3L等)出现在R代码中。 1L和1什么区别? 1==1L计算结果为TRUE 。 为什么在R代码中使用1L ? 所以,@詹姆斯和@布莱恩解释了 3L的含义。 但是,你为什么要使用它? 大多数时候它没有什么区别 - 但有时你可以使用它来让你的代码运行得更快并且消耗更少的内存。 双(“数字”)向量每个元素使用8个字节。 整数向量每个元素只使用4个字节。 对于大型向量来说,内存浪费较少,而且CPU的吞吐量也较
Possible Duplicate: Assignment operators in R: '=' and '<-' I was wondering if there is a technical difference between the assignment operators "=" and "<-" in R. So, does it make any difference if I use: Example 1: a = 1 or a <- 1 Example 2: a = c(1:20) or a <- c(1:20) Thanks for your help Sven Yes there is. This is what the help pag
可能重复: R中的赋值运算符:'='和'< - ' 我想知道R中的赋值运算符"="和"<-"之间是否存在技术差异。因此,如果我使用以下内容,是否会有所不同? 例1: a = 1或a <- 1 例2: a = c(1:20)或a <- c(1:20) 谢谢你的帮助 斯文 就在这里。 这就是'='的帮助页面所说的内容: 运算符< - 和=分配到它们被评估的环境中。 运算符< - 可以在任何地方使用
This question already has an answer here: What are the differences between “=” and “<-” in R? 6 answers The <- operator not only assigns objects but creates them in the parent environment unlike = operator Renaming columns slightly: df1 <- data.frame(a1 = 1:5, b1 = 11:15) df1 # a1 b1 # 1 1 11 # 2 2 12 # 3 3 13 # 4 4 14 # 5 5 15 #The objects are only created in the datafra
这个问题在这里已经有了答案: R中“=”和“< - ”之间有什么区别? 6个答案 <- =运算符不仅分配对象,而且在父环境中创建它们,这与=运算符不同 稍微重命名列: df1 <- data.frame(a1 = 1:5, b1 = 11:15) df1 # a1 b1 # 1 1 11 # 2 2 12 # 3 3 13 # 4 4 14 # 5 5 15 #The objects are only created in the dataframee but not in environment # > exists(x = "a1") #[1] FALSE #> exists(x = "b1")
This question already has an answer here: What are the differences between “=” and “<-” in R? 6 answers It is because <- and = are not the same. For (just about) every purpose except when used inside a function call , <- and = are the same. If you want to pass a named argument, you have to use = not <- . myFunction(X <- expression) will evaluate expression and save the resu
这个问题在这里已经有了答案: R中“=”和“< - ”之间有什么区别? 6个答案 这是因为<-和=不一样。 对于除了在函数调用中使用的每个目的(几乎), <-和=是相同的。 如果你想传递一个命名参数,你必须使用= not <- 。 myFunction(X <- expression)将计算expression并将结果保存到(全局)变量X ,并将结果作为myFunction的第一个( 未命名 )参数。 与myFunction(X = expression) ,它将表达式作为指定的 X