How to average score from different elements

This question already has an answer here: Mean per group in a data.frame [duplicate] 8 answers Aggregate / summarize multiple variables per group (eg sum, mean) 4 answers How to make a great R reproducible example? 23 answers 我们可以尝试library(data.table) setDT(data)[, list(avg = mean(Score)), by = AD] library(data.table) dt <- data.table(ur_data) dt[, lapply(.SD, mean), by = "AD"]

如何平均来自不同元素的分数

这个问题在这里已经有了答案: 平均每个组在data.frame中8个答案 汇总/汇总每个组的多个变量(例如总和,平均值)4个答案 如何做一个伟大的R可重现的例子? 23个答案 我们可以尝试library(data.table) setDT(data)[, list(avg = mean(Score)), by = AD] library(data.table) dt <- data.table(ur_data) dt[, lapply(.SD, mean), by = "AD"] 我会推荐dplyr软件包。 require(dplyr) data %>% group_by(AD) %>

Creating a vector of random samples of variable length

I have an R script where l2 is an array of all permutations of three letter combinations az ie the first values is aaa and the last values is zzz c <- rpois(n, 5) is a vector of poisson distribution results. I want to create a data frame that containtns a random number of samples from l2 and concatenates them into a path like this: abc -> vye -> tyb for 3. df_p <- data.frame('l

创建一个可变长度的随机样本矢量

我有一个R脚本在哪里 l2是三个字母组合的所有排列的数组,即第一个值是aaa ,最后一个值是zzz c <- rpois(n, 5)是泊松分布结果的向量。 我想创建一个数据框,包含来自l2的随机数的样本,并将它们连接成如下路径: abc -> vye -> tyb for 3。 df_p <- data.frame('len' = c, 's1' = paste(sample(l2,c), collapse = ' -> ')) 我试图使用上面的行来创建它,但它为每一行返回相同的路径。 该路径的长度对应

R: Random number between range if value in another vector is between x and y

I have a vector ACCNS in a data.frame E. ACCNS has discrete values 0, 1, 5, 12, 26 or 40. I'd like to make another vector ACCNSrandom that has a 'runif' value based on 0-1, 1-5, 5-12, 12-26, 26-40 and 40-100. I've tried this with a nested ifelse but I get the same value each time (as reported here). I can't work out how to apply the answer given in that post to a more genera

R:如果另一个矢量中的值在x和y之间,则范围之间的随机数

我有一个矢量ACCNS在data.frame E. ACCNS具有离散值0,1,5,12,26或40.我想制作另一个具有基于0-1的'runif'值的矢量ACCNSrandom, 1-5,5-12,12-26,26-40和40-100。 我已经尝试过嵌套ifelse,但每次都获得相同的值(如此处所述)。 我无法解决如何将这篇文章中给出的答案应用到更一般的形式。 任何帮助将非常感激。 E<-data.frame(ACCNS=sample(c(0,1,2.5,5,12,26,40),50,replace = T)) E$ACCNSrandom <- ifelse

Round numbers at a threshold value in R

I'm trying to do logical regression and I got to the point where I have the probability for each observation. Now I would like to classify the probabilities to either 0 or 1 given a threshold value For example, if I have two numbers 0.65 and 0.87 and my threshold is 0.7, I'd like to round 0.65 to 0 and 0.87 to 1. To achieve this, I've tried the following code which I think is too

R处的阈值处的整数

我试图进行逻辑回归,并且达到了每次观察都有可能性的地步。 现在我想给出一个阈值的概率分为0或1 例如,如果我有两个数字0.65和0.87,并且我的阈值为0.7,我想将0.65舍入到0和0.87到1。 为了实现这一点,我尝试了下面的代码,我认为这对于这样一个简单的任务来说太多了,我想知道是否有任何专用于执行此操作的功能。 library(tidyverse) # create a table of probabilities and predictions (0 or 1) df <- tibble(

Converting fractions to decimals in an R vector

This question already has an answer here: How to perform arithmetic on values and operators expressed as strings? 1 answer Finally found that solution: x = c(1/2,5/2,7/2) frac <- factor(x) as.numeric(levels(frac))[frac] Works with x = c("1/2","5/2","7/2") too The other problem is that you have labels that differ from values You want to convert labels to d

将分数转换为R向量中的小数

这个问题在这里已经有了答案: 如何对以字符串表示的值和运算符进行算术运算? 1个答案 终于找到解决办法: x = c(1/2,5/2,7/2) frac <- factor(x) as.numeric(levels(frac))[frac] 同样适用于x = c("1/2","5/2","7/2") 另一个问题是,您的标签与您想要将标签转换为十进制值的值不同。 然后使用 frac <- structure(c(14L, 13L, 4L, 5L, 8L, 7L, 3L, 8L, 11L, 1L), .Label = c("10

Dynamically adding values to dynamically created vectors

I just started learning to code in R. I have a requirement where I have to keep adding unknown number of values to different vectors (number of vectors is not known). So, I tried to implement this using - clust_oo = c() clust_oo[k] = c(clust_oo[k],init_dataset[k,1]) Without the [k] , the above code works but since i don't know the number of vectors/lists i have to use [k] as a differentia

动态添加值到动态创建的向量

我刚开始学习编码R.我有一个要求,我必须不断向不同的向量添加未知数量的值(向量数量未知)。 所以,我试图实现这个使用 - clust_oo = c() clust_oo[k] = c(clust_oo[k],init_dataset[k,1]) 没有[k] ,上面的代码可以工作,但是因为我不知道向量/列表的数量,所以我必须使用[k]作为区分。 clust_oo[1]可具有值说, 1 , 23 , 45 , clust_oo[2]可以具有其它值4 , 40和clust_oo[3]与值44 , 67 , 455 , 885 。 这些值

generate a random vector with specific number of negative numbers

I'm doing a failure analysis, for which I like to try some different scenarios and some random trials. So far I've done this with the mosaic package and its working out great. In one specific scenario I want to generate a vector of (semi)random numbers with from different distributions. No problem so far. Now I want to have defined number of negative numbers in this vector. For exam

生成一个具有特定数量负数的随机向量

我正在做一个失败分析,我喜欢尝试一些不同的场景和一些随机试验。 到目前为止,我已经通过镶嵌套件完成了这项工作,并且工作效果很好。 在一个特定的场景中,我想用不同的分布生成一个(半)随机数的向量。 到目前为止没有问题。 现在我想定义此向量中负数的数量。 例如,我想在25个数字的向量中有0-5个负数。 我以为我可以使用像rbinom(n=25,prob=5/25,size=1)来获得5个随机数,但当然是rbinom(n=25,prob=5/25,size=1)

How do you create vectors with specific intervals in R?

I have a question about creating vectors. If I do a <- 1:10 , "a" has the values 1,2,3,4,5,6,7,8,9,10. My question is how do you create a vector with specific intervals between its elements. For example, I would like to create a vector that has the values from 1 to 100 but only count in intervals of 5 so that I get a vector that has the values 5,10,15,20,...,95,100 I think that

你如何在R中创建具有特定间隔的向量?

我有一个关于创建矢量的问题。 如果我做a <- 1:10 ,“a”的值为1,2,3,4,5,6,7,8,9,10。 我的问题是如何创建一个具有特定元素间隔的矢量。 例如,我想创建一个具有从1到100的值的向量,但只计算5的间隔,以便得到具有值5,10,15,20,...,95,100的向量 我认为在Matlab中我们可以做1:5:100 ,我们如何使用R来做到这一点? 我可以尝试做5*(1:20)但有一个更短的路? (因为在这种情况下,我需要知道整个长度(100),然后

Creating vector of results of repeated function calls in R

I have a function that uses runif to calculate some value, so each time it is called, the result varies slightly. I want to calculate the mean of the result of several calls to the function. For this, it would be great to create a vector with the results of repeated function calls Is there a simple idiomatic way to create a vector of repeated function calls? I tries rep(my_function_call(),

在R中创建重复函数调用结果的向量

我有一个使用runif来计算某个值的函数,所以每次调用它时,结果都会略有不同。 我想计算几次函数调用结果的平均值。 为此,创建一个具有重复函数调用结果的向量将是非常好的 有没有一种简单的惯用方法来创建重复函数调用的向量? 我尝试 rep(my_function_call(), 10) 但它只是调用一次函数并重复10次结果。 我希望函数评估10次,并且结果的向量。 replicate是你的朋友。 请参阅?replicate replicate(10, my_function

Counting the number of elements with the values of x in a vector

I have a vector of numbers: numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435, 453,435,324,34,456,56,567,65,34,435) How can I have R count the number of times a value x appears in the vector? You can just use table() : > a <- table(numbers) > a numbers 4 5 23 34 43 54 56 65 67 324 435 453 456 567 657 2 1 2 2 1 1 2 1 2 1 3 1 1 1 1

计算矢量中x的值的元素数量

我有一个数字矢量: numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435, 453,435,324,34,456,56,567,65,34,435) 我如何让R计算向量中出现值x的次数? 你可以使用table() : > a <- table(numbers) > a numbers 4 5 23 34 43 54 56 65 67 324 435 453 456 567 657 2 1 2 2 1 1 2 1 2 1 3 1 1 1 1 然后你可以对它进行分类: > a[names(a)==435] 435