the function "trio" exemplifies a function with numerous calculations, that takes in certain columns from a data frame (df) and three values for the calculations: # example: data df <- data.frame(C1= c(1,2,3,4,5,6),C2= c(5,4,3,2,1,6),C3= c(9,1,5,2,7,4)) # example: function trio <- function(a,b,c){ df2 <- df %>% mutate(., df$C1 *
函数“三重奏”举例说明了一个具有大量计算的函数,它从数据框(df)中取出某些列,并计算三个值: # example: data df <- data.frame(C1= c(1,2,3,4,5,6),C2= c(5,4,3,2,1,6),C3= c(9,1,5,2,7,4)) # example: function trio <- function(a,b,c){ df2 <- df %>% mutate(., df$C1 * a, df$C2 * b, df$C2 * c ) df2$c
I have checked and I found several questions related to this questions multiple functions in a single tapply or aggregate statement R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate Actually I want to know what is the best way to use multiple functions in one of the above mentioned algorithms. I try to give an example # make a simple matrix df <- matrix(
我已经检查过,并且发现了几个与这个问题有关的问题,在单个tapply或汇总语句中提供了多个函数R分组函数:sapply vs. lapply vs. apply。 与tapply与vs. aggregate的比较 其实我想知道在上述算法之一中使用多个函数的最佳方法是什么。 我试着举个例子 # make a simple matrix df <- matrix(data=rnorm(10), 10, 5) # make a function which calculate several properties several <- function(x) { c(min =
I'm trying to understand how to aggregate my output. I've created some dummy data which approximates my actual data, which is: hundreds of group1, 3 levels of group2, and several dozen validation logicals. Apologies if this seems simple, I've hunted and pecked alot, and have to say that as a newbie to R, the huge variety of tools (the apply family, ddply, aggregate, table, reshape,
我试图了解如何汇总我的输出。 我创建了一些虚拟数据,这些虚拟数据近似于我的实际数据,即:数百个group1,group2的3个级别以及数十个验证逻辑。 道歉,如果这看起来很简单,我已经狩猎和啄很多,不得不说,作为R的新手,各种各样的工具(申请家庭,ddply,聚合,表,重塑等)都是美妙的有点可怕:) #create data group1 <- paste("Group", rep(LETTERS[1:7], sep='')) group2 <- c("UNC", "UNC", "SS", "LS", "LS", "S
I'm trying to calculate the mean number of unique fruits per person (my usual practice data). This works perfectly well with both these lines of code: with(df, tapply(fruit, names, FUN = function(x) length(unique(x))))->uniques sum(uniques)/length(unique(df$names)) aggregate(df[,"fruit"], by=list(id=names), FUN = function(x) length(unique(x)))->d1 sum(d1$x)/length(unique(df$names))
我试图计算每人独特水果的平均数量(我通常的练习数据)。 这与这两行代码完美兼容: with(df, tapply(fruit, names, FUN = function(x) length(unique(x))))->uniques sum(uniques)/length(unique(df$names)) aggregate(df[,"fruit"], by=list(id=names), FUN = function(x) length(unique(x)))->d1 sum(d1$x)/length(unique(df$names)) 我的问题是,当我在我的真实数据上使用代码时,它不起作用。 我的真实数据是处
I am very new to R and stats in general and am having trouble getting tapply() to work. I have a data frame with 15 columns and thousands of rows. I made a bunch of logical vectors using something like y1<-((x>0)&(x<=5)) and similar, where x is a column name in the data frame. These logical vectors were then combined and converted into a grouping factor using factor(). Everything
我对R和统计数据非常陌生,并且无法让tapply()工作。 我有一个有15列和数千行的数据框。 我用y1<-((x>0)&(x<=5))和类似的东西做了一堆逻辑向量,其中x是数据框中的列名。 然后将这些逻辑向量组合并使用因子()将其转换为分组因子。 一切看起来都可以正常工作。 问题是,当我尝试在tapply(dataframe, group, sample, size=20)中使用tapply(),其中group是分组因子时,我得到错误:'参数必须具有相同
Possible Duplicate: R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate vs I have a model output file which looks like: run step x 1 1 1 1 2 4 1 3 3 1 1 4 1 2 5 1 3 6 2 1 5 2 2 4 2 3 7 2 1 3 . . . and I need to calculate the mean values for each step according to the run number.How can I do this?
可能重复: R分组功能:sapply vs. lapply vs. apply。 vs tapply vs. by aggregate vs vs 我有一个模型输出文件,如下所示: run step x 1 1 1 1 2 4 1 3 3 1 1 4 1 2 5 1 3 6 2 1 5 2 2 4 2 3 7 2 1 3 。 。 。 我需要根据跑步数来计算每一步的平均值。我可以怎样做? 非常感谢任何人,谁可以帮助我。 中提琴 如果我理解正确,可以使用plyr软件包
After using sapply(data, unlist), I have what I think is a matrix of vectors or maybe it's still a list. The output looks like: [[index1]] c1 "value" c2 "value" c3 "value" [[index2]] c1 "value" c2 "value" c3 "value" My desired output is for each index to become a row in a data frame with "c1", "c2", and &quo
在使用sapply(data,unlist)之后,我有我认为是矢量矩阵或者它仍然是一个列表。 输出如下所示: [[index1]] c1 "value" c2 "value" c3 "value" [[index2]] c1 "value" c2 "value" c3 "value" 我希望的输出是为每个索引成为数据框中的一行,其中“c1”,“c2”和“c3”列作为列。 我已经尝试了一个简单的转置()。 我想我可以使用重塑或plyr来做到这一点,但我
You know how you can supply a vector of names to a data frame to change the col or row names of a dataframe. Is there a similar method to supply a vector of names that alters the class of each column in a dataframe? You can do this when you read in a dataframe with read.table using colClasses. What about if the dataframe is created inside R? DF <- as.data.frame(matrix(rnorm(25), 5, 5)) str
您知道如何为数据框提供名称向量来更改数据框的列名或列名。 有没有类似的方法来提供一个名称矢量,以改变数据框中每列的类别? 你可以在使用colClasses读取带有read.table的数据框时执行此操作。 如果数据框是在R内创建的呢? DF <- as.data.frame(matrix(rnorm(25), 5, 5)) str(DF) #all numeric modes names(DF) <- c("A", "A2", "B", "B2", "Z") #I want something like this for classes some_classes_function_
I understand what tapply() does in R. However, I cannot parse this description of it from the documentaion: Apply a Function Over a "Ragged" Array Description: Apply a function to each cell of a ragged array, that is to each (non-empty) group of values given by a unique combination of the levels of certain factors. Usage: tapply(X, INDEX, FUN = NULL, ..., simplify = TRU
我明白在R中tapply()是做什么的。但是,我无法从文档中解析它的描述: Apply a Function Over a "Ragged" Array Description: Apply a function to each cell of a ragged array, that is to each (non-empty) group of values given by a unique combination of the levels of certain factors. Usage: tapply(X, INDEX, FUN = NULL, ..., simplify = TRUE) 当我想到tapply时,我想通过sql来分组
Whenever I want to do something "map"py in R, I usually try to use a function in the apply family. However, I've never quite understood the differences between them -- how { sapply , lapply , etc.} apply the function to the input/grouped input, what the output will look like, or even what the input can be -- so I often just go through them all until I get what I want. Can someon
每当我想在R中做一些“映射”py时,我通常会尝试在apply系列中使用函数。 但是,我从来没有完全理解它们之间的差异 - 如何将输入/分组输入应用到函数,输出将会是什么样子,甚至输入什么样的东西,所以{ sapply , lapply等}我经常只是经历他们,直到我得到我想要的。 有人可以解释如何使用哪一个? 我目前(可能不正确/不完整)的理解是...... sapply(vec, f) :输入是一个向量。 输出是一个向量/矩阵,其中元素i是f(vec