计算按ID分组的多行的标准差

时间:2020-07-18 15:23:21

标签: r tidyverse

我想计算多行(不是每行)的标准差,然后将结果保存到新的数据框中。最好用一个例子来解释。

数据:

ID <- c("a","a","a","a","b","b","b","b","c","c","c","c")
y1 <- c(8,9,3,6,6,4,5,8,7,5,8,1)
y2 <- c(3,6,6,1,7,3,8,7,5,8,1,7)
y3 <- c(9,3,1,8,4,6,3,8,4,6,5,7)
df <- data.frame(ID, y1, y2, y3)

   ID y1 y2 y3
1   a  8  3  9
2   a  9  6  3
3   a  3  6  1
4   a  6  1  8
5   b  6  7  4
6   b  4  3  6
7   b  5  8  3
8   b  8  7  8
9   c  7  5  4
10  c  5  8  6
11  c  8  1  5
12  c  1  7  7

我想计算ID $ a,ID $ b和ID $ c的标准偏差,并存储在新的数据框中。我知道我可以做到:

sd_a <- sd(as.matrix(subset(df, ID == "a")), na.rm = TRUE)
sd_b <- sd(as.matrix(subset(df, ID == "b")), na.rm = TRUE)
sd_c <- sd(as.matrix(subset(df, ID == "c")), na.rm = TRUE)

ID <- c("a","b","c")
sd <- c(sd_a,sd_b,sd_c)
df2 <- data.frame(ID, sd)

  ID       sd
1  a 2.958040
2  b 1.912875
3  c 2.386833

但是有没有更简单的方法来实现这一目标?

3 个答案:

答案 0 :(得分:2)

一种 <a class="twitter-timeline" href="https://twitter.com/msdev">Tweets by Microsoft Developer</a> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> 解决方案可能是:

dplyr

答案 1 :(得分:2)

您可以使用#include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <sched.h> void *task() { printf("I am a simple thread.\n"); pthread_exit(NULL); } int main() { pthread_attr_t attr; // Struttura degli attributi di UN SINGOLO thread struct sched_param prio; // Predefinita in sched.h, contiene solo il campo int sched_priority; valori interi consentiti: [1, 99], 99 priorità massima pthread_t tid; // Identificatore del thread (sarà la pthread_create ad assegnarlo) int create = 1; // Intero che userò per testare se la pthread_create ha creato correttamente il thread int scheduler = 1; // Intero che userò per testare la pthread_attr_setinheritsched int policy = 1; // Intero che userò per testare la pthread_attr_setschedpolicy int setschedparam = 1; // Intero che userò per testare la pthread_attr_setschedparam int getschedparam = 1; // Intero che userò per testare la pthread_attr_getschedparam /* Inizializzo la struttura degli attributi attr con i valori di default degli attributi del thread */ pthread_attr_init(&attr); // Imposto schedulatore SCHED_FIFO scheduler = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //printf("%d\n", scheduler); // 0 OK if (scheduler != 0) exit(EXIT_FAILURE); policy = pthread_attr_setschedpolicy(&attr, SCHED_FIFO); if (policy != 0) exit(EXIT_FAILURE); //printf("%d\n", policy); // 0 OK // Imposto il livello di priorità del thread prio.sched_priority = 12; setschedparam = pthread_attr_setschedparam(&attr, &prio); //printf("%d\n", setschedparam); // 0 OK if (setschedparam != 0) exit(EXIT_FAILURE); // Ingressi pthread_create(id restituito dalla pthread_create, struttura attributi del thread, funzione da trasformare in thread, argomenti da passare al thread) create = pthread_create(&tid, &attr, task, NULL); printf("%d\n", create); // 1 KO if (create != 0) exit(EXIT_FAILURE); getschedparam = pthread_attr_getschedparam(&attr, &prio); if (getschedparam != 0) exit(EXIT_FAILURE); printf("Livello di priorità del thread: %d\n", prio.sched_priority); // Uscita: 0 KO pthread_join(tid, NULL); return(0); } pivot_longer()堆叠到y1,然后计算y3

sd

答案 2 :(得分:1)

在R中,您可以执行以下操作:

aggregate(values ~ ID, cbind(df[1], stack(df[-1])), sd)

  ID   values
1  a 2.958040
2  b 1.912875
3  c 2.386833