lagging in data.table R

Currently I have a utility function that lags things in data.table by group. The function is simple: panel_lag <- function(var, k) { if (k > 0) { # Bring past values forward k times return(c(rep(NA, k), head(var, -k))) } else { # Bring future values backward return(c(tail(var, k), rep(NA, -k))) } } I can then call this from a data.table : x = data.table(a=1:10,

滞后于data.table R

目前,我有一个实用功能,该功能lags于data.table的组。 功能很简单: panel_lag <- function(var, k) { if (k > 0) { # Bring past values forward k times return(c(rep(NA, k), head(var, -k))) } else { # Bring future values backward return(c(tail(var, k), rep(NA, -k))) } } 然后我可以从data.table调用它: x = data.table(a=1:10, dte=sample(seq.Date(from=as.Date

How to furnish a ggplot2 figure with a hyperlink?

I am trying to furnish a ggplot2 plot with a hyperlink: This works: library(gridSVG) library(lattice) xyplot(mpg~wt, data=mtcars, main = "Link to R-project home") mainGrobName <- grep("main", grid.ls()[[1]], value=TRUE) grid.hyperlink(mainGrobName, "http://www.r-project.org") gridToSVG("HyperlinkExample.svg") This not: p = ggplot(mtcars, aes(wt, mpg)) + geom_point()+ labs(title="link") p

如何用超链接提供ggplot2图形?

我正在尝试用超链接提供ggplot2图: 这工作: library(gridSVG) library(lattice) xyplot(mpg~wt, data=mtcars, main = "Link to R-project home") mainGrobName <- grep("main", grid.ls()[[1]], value=TRUE) grid.hyperlink(mainGrobName, "http://www.r-project.org") gridToSVG("HyperlinkExample.svg") 这不是: p = ggplot(mtcars, aes(wt, mpg)) + geom_point()+ labs(title="link") print(p) mainGrobName <-

Convert row data to binary columns

I am attempting to format a column of data into many binary columns to eventually use for association rule mining. I have had some success using a for loop and a simple triplet matrix, but I am unsure how to aggregate by the levels in the first column thereafter--similar to a group by statement in SQL. I have provided an example below, albeit with a much smaller data set--if successful my actua

将行数据转换为二进制列

我试图将一列数据格式化为许多二进制列,最终用于关联规则挖掘。 使用for循环和简单的三元组矩阵已经取得了一些成功,但我不确定如何根据第一列中的级别进行聚合 - 类似于SQL中的group by语句。 我在下面提供了一个示例,尽管数据集小得多 - 如果成功,我的实际数据集将是4,200行3,902列,因此任何解决方案都需要可扩展。 任何建议或替代方法将不胜感激! > data <- data.frame(a=c('sally','george','andy','sue','sue

To add new value in every element in list in R?

here is list1 , only tow elements--" name " and " age " in it,there are two value in every element ,now i want to add new value in every element, list1<-list(name=c("bob","john"),age=c(15,17)) list1 $name [1] "bob" "john" $age [1] 15 17 list1[[1]][3]<-"herry" list1[[2]][3]<-17 list1 $name [1] "bob" "john" "herry" $age [1] 15 17 17 is there more simple way to d

在R中列表中的每个元素中添加新值?

这里是list1 ,只有两个元素 - “ name ”和“ age ”,其中每个元素都有两个值,现在我想在每个元素中添加新值, list1<-list(name=c("bob","john"),age=c(15,17)) list1 $name [1] "bob" "john" $age [1] 15 17 list1[[1]][3]<-"herry" list1[[2]][3]<-17 list1 $name [1] "bob" "john" "herry" $age [1] 15 17 17 有更简单的方法吗? 此解决方案适用于任何长度的列表: values <- list("herry", 17) # a list

Is rpart automatic pruning?

Is rpart automatic pruning? The decision tree produced by rpart is much more levels than that produced by Oracle Data Mining which has the automatic pruning. No, but the defaults for the fitting function may stop splitting "early" (for some definition of "early"). See ?rpart.control for the parameters you can tweak. In particular, see the argument minsplit and minbucket

rpart是否自动修剪?

rpart是否自动修剪? 由rpart生成的决策树比由具有自动修剪的Oracle Data Mining生成的决策树要高得多。 否,但适配功能的默认设置可能会停止“早期”分裂(对于“早期”的某些定义)。 请参阅?rpart.control了解您可以调整的参数。 特别是,请参阅该帮助文件中的参数minsplit和minbucket 。 这些是停止规则,如果这些条件不满足,将阻止任何节点被分割。 您很可能需要使用prune()将树修剪回成本复杂度参数的某个最优值。

as an assignment operator (like =)

I code in R with sublime text2. When I assign a variable using = , the left member is colored. I would like it to be the same when assigning with the arrow <- I can't find the corresponding setting in the default setting-file, how can this be tuned?

作为赋值运算符(如=)

我使用崇高的text2编码R。 当我使用=来赋值变量时,左边的成员是彩色的。 我希望它是相同的,当分配箭头< - 我找不到默认设置文件中的对应设置,如何调整?

Add new row to dataframe, at specific row

The following code combines a vector with a dataframe: newrow = c(1:4) existingDF = rbind(existingDF,newrow) However this code always inserts the new row at the end of the dataframe. How can I insert the row at a specified point within the dataframe? For example, lets say the dataframe has 20 rows, how can I insert the new row between rows 10 and 11? Here's a solution that avoids the (

在特定行中向数据框添加新行

以下代码将矢量与数据框结合在一起: newrow = c(1:4) existingDF = rbind(existingDF,newrow) 但是,此代码始终将新行插入数据帧的末尾。 如何在数据框内的指定点插入行? 例如,假设数据框有20行,我如何在第10行和第11行之间插入新行? 这是一个避免(通常很慢) rbind调用的解决方案: existingDF <- as.data.frame(matrix(seq(20),nrow=5,ncol=4)) r <- 3 newrow <- seq(4) insertRow <- function(exist

How to rename a single column in a data.frame?

I know if I have a data frame with more than 1 column, I can use colnames(x) <- c("col1","col2") to rename the columns. How do I do this if it's just one column? Meaning a vector or data frame with only one column in it. Example: trSamp <- data.frame(sample(trainer$index, 10000)) head(trSamp ) # sample.trainer.index..10000. # 1 5907862 # 2

如何重命名data.frame中的单个列?

我知道如果我有超过1列的数据框,我可以使用 colnames(x) <- c("col1","col2") 重新命名列。 如果只有一列,我该怎么做? 意味着只有一列的矢量或数据框。 例: trSamp <- data.frame(sample(trainer$index, 10000)) head(trSamp ) # sample.trainer.index..10000. # 1 5907862 # 2 2181266 # 3 7368504 # 4 1949790 # 5

Changing column names of a data frame

I have a data frame called "newprice" (see below) and I want to change the column names in my program in R. > newprice Chang. Chang. Chang. 1 100 36 136 2 120 -33 87 3 150 14 164 In fact this is what am doing: names(newprice)[1]<-paste("premium") names(newprice)[2]<-paste("change") names(newprice)[3]<-paste("newprice")

更改数据框的列名称

我有一个名为“newprice”的数据框(见下文),我想在R中的程序中更改列名。 > newprice Chang. Chang. Chang. 1 100 36 136 2 120 -33 87 3 150 14 164 其实这就是我在做的事情: names(newprice)[1]<-paste("premium") names(newprice)[2]<-paste("change") names(newprice)[3]<-paste("newprice") 我没有把它放在一个循环中,因为我希望每个列的名称与您

Running advanced MongoDB queries in R with rmongodb

As MySQL is driving me nuts I'm trying to make myself acquainted with my first "NoSQL" DBMS and it happened to be MongoDB. I'm connecting to it via rmongodb. The more I play around with rmongodb, the more questions/problems come up with respect to running advanced queries. First I present some example data before I go into detail about the different types of queries that I

用rmongodb在R中运行高级MongoDB查询

由于MySQL让我疯狂,我试图让自己熟悉我的第一个“NoSQL”DBMS,它碰巧是MongoDB。 我通过rmongodb连接到它。 我用rmongodb玩的越多,就运行高级查询而言出现的问题就越多。 首先,我介绍一些示例数据,然后详细介绍我无法正确指定的不同类型的查询。 示例数据 这个例子取自MongoDB网站,并已简化了一些。 pkg <- "rmongodb" if (!require(pkg, character.only=TRUE)) { install.packages(pkg) require(pkg, c