Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中按组求和变量_R_Database_Dataframe - Fatal编程技术网

如何在R中按组求和变量

如何在R中按组求和变量,r,database,dataframe,R,Database,Dataframe,我有一个名为“SpatialKey”的数据帧,有三列。第一列包含代表人口五分位数的5个类别。第二列有4种数据:0、400、800和1200。第三列代表人口 比如说 五等分 等回旋线 全部的 4. 1200 1674 1. 400 1676 4. 400 1723 5. 800 1567 3. 0 1531 3. 1200 1370 2. 1200 1925 1. 400 1916 5. 0 1776 2. 800 1896 3. 800 2143 5. 400 2098 4. 400 1496

我有一个名为“SpatialKey”的数据帧,有三列。第一列包含代表人口五分位数的5个类别。第二列有4种数据:0、400、800和1200。第三列代表人口

比如说

五等分 等回旋线 全部的 4. 1200 1674 1. 400 1676 4. 400 1723 5. 800 1567 3. 0 1531 3. 1200 1370 2. 1200 1925 1. 400 1916 5. 0 1776 2. 800 1896 3. 800 2143 5. 400 2098 4. 400 1496 1. 0 961 4. 800 1684
在这里,我们需要一个
pivot\u wide
来在执行
sum

library(dplyr)
library(tidyr)
SpatialKey %>%
    arrange(quintile, isocrona) %>%
    pivot_wider(names_from = isocrona, values_from = total, 
        values_fn = sum, values_fill = 0)
-输出

# A tibble: 5 x 5
#  quintile   `0` `400` `800` `1200`
#     <int> <int> <int> <int>  <int>
#1        1   961  3592     0      0
#2        2     0     0  1896   1925
#3        3  1531     0  2143   1370
#4        4     0  3219  1684   1674
#5        5  1776  2098  1567      0
数据
SpatialKey这里我们需要一个
pivot\u-wide
在执行
sum

library(dplyr)
library(tidyr)
SpatialKey %>%
    arrange(quintile, isocrona) %>%
    pivot_wider(names_from = isocrona, values_from = total, 
        values_fn = sum, values_fill = 0)
-输出

# A tibble: 5 x 5
#  quintile   `0` `400` `800` `1200`
#     <int> <int> <int> <int>  <int>
#1        1   961  3592     0      0
#2        2     0     0  1896   1925
#3        3  1531     0  2143   1370
#4        4     0  3219  1684   1674
#5        5  1776  2098  1567      0
数据
SpatialKey使用xtabs。将要求和的变量放在公式的左侧,其他变量放在右侧。我们可以用点来表示所有其他的。没有使用任何软件包

xtabs(total ~., SpatialKey)
给出这个xtabs表:

        isocrona
quintile    0  400  800 1200
       1  961 3592    0    0
       2    0    0 1896 1925
       3 1531    0 2143 1370
       4    0 3219 1684 1674
       5 1776 2098 1567    0
注 可复制形式的输入为:

SpatialKey <- structure(list(quintile = c(4L, 1L, 4L, 5L, 3L, 3L, 2L, 1L, 5L, 
2L, 3L, 5L, 4L, 1L, 4L), isocrona = c(1200L, 400L, 400L, 800L, 
0L, 1200L, 1200L, 400L, 0L, 800L, 800L, 400L, 400L, 0L, 800L), 
    total = c(1674L, 1676L, 1723L, 1567L, 1531L, 1370L, 1925L, 
    1916L, 1776L, 1896L, 2143L, 2098L, 1496L, 961L, 1684L)), 
    class = "data.frame", row.names = c(NA, -15L))

SpatialKey使用xtabs。将要求和的变量放在公式的左侧,其他变量放在右侧。我们可以用点来表示所有其他的。没有使用任何软件包

xtabs(total ~., SpatialKey)
给出这个xtabs表:

        isocrona
quintile    0  400  800 1200
       1  961 3592    0    0
       2    0    0 1896 1925
       3 1531    0 2143 1370
       4    0 3219 1684 1674
       5 1776 2098 1567    0
注 可复制形式的输入为:

SpatialKey <- structure(list(quintile = c(4L, 1L, 4L, 5L, 3L, 3L, 2L, 1L, 5L, 
2L, 3L, 5L, 4L, 1L, 4L), isocrona = c(1200L, 400L, 400L, 800L, 
0L, 1200L, 1200L, 400L, 0L, 800L, 800L, 400L, 400L, 0L, 800L), 
    total = c(1674L, 1676L, 1723L, 1567L, 1531L, 1370L, 1925L, 
    1916L, 1776L, 1896L, 2143L, 2098L, 1496L, 961L, 1684L)), 
    class = "data.frame", row.names = c(NA, -15L))

SpatialKey一种基于
group
思想的方法。好处是结果仍然是数据帧格式

长格式的结果:

library(data.table)
dt.long <- setDT(SpatialKey)[,sum(total),keyby = .(quintile,isocrona)]
dt.long

   quintile isocrona   V1
 1:        1        0  961
 2:        1      400 3592
 3:        2      800 1896
 4:        2     1200 1925
 5:        3        0 1531
 6:        3      800 2143
 7:        3     1200 1370
 8:        4      400 3219
 9:        4      800 1684
10:        4     1200 1674
11:        5        0 1776
12:        5      400 2098
13:        5      800 1567
数据:


SpatialKey一种基于
group
思想的方法。好处是结果仍然是数据帧格式

长格式的结果:

library(data.table)
dt.long <- setDT(SpatialKey)[,sum(total),keyby = .(quintile,isocrona)]
dt.long

   quintile isocrona   V1
 1:        1        0  961
 2:        1      400 3592
 3:        2      800 1896
 4:        2     1200 1925
 5:        3        0 1531
 6:        3      800 2143
 7:        3     1200 1370
 8:        4      400 3219
 9:        4      800 1684
10:        4     1200 1674
11:        5        0 1776
12:        5      400 2098
13:        5      800 1567
数据:

SpatialKey