R语言数据重塑

R中的数据重整是关于将数据组织成行和列的方式。 R中的大多数时间数据处理是通过将输入数据作为数据帧来完成的。 很容易从数据帧的行和列中提取数据,但是有些情况下,我们需要的格式与收到的格式不同。 R具有许多函数,用于在数据帧中拆分,合并和更改行到列,反之亦然。

在数据框中连接列和行

我们可以使用cbind()函数连接多个向量来创建数据帧。也可以使用rbind()函数合并两个数据帧。

# Create vector objects.
city <- c("Tampa","Seattle","Hartford","Denver")
state <- c("FL","WA","CT","CO")
zipcode <- c(33602,98104,06161,80294)

# Combine above three vectors into one data frame.
addresses <- cbind(city,state,zipcode)

# Print a header.
cat("# # # # The First data frame\n") 

# Print the data frame.
print(addresses)

# Create another data frame with similar columns
new.address <- data.frame(
   city = c("Lowry","Charlotte"),
   state = c("CO","FL"),
   zipcode = c("80230","33949"),
   stringsAsFactors = FALSE
)

# Print a header.
cat("# # # The Second data frame\n") 

# Print the data frame.
print(new.address)

# Combine rows form both the data frames.
all.addresses <- rbind(addresses,new.address)

# Print a header.
cat("# # # The combined data frame\n") 

# Print the result.
print(all.addresses)

当我们执行上述代码时,会产生以下结果 -

# # # # The First data frame
     city       state zipcode
[1,] "Tampa"    "FL"  "33602"
[2,] "Seattle"  "WA"  "98104"
[3,] "Hartford" "CT"   "6161" 
[4,] "Denver"   "CO"  "80294"

# # # The Second data frame
       city       state   zipcode
     Lowry      CO      80230
     Charlotte  FL      33949

# # # The combined data frame
       city      state zipcode
     Tampa     FL    33602
     Seattle   WA    98104
     Hartford  CT     6161
     Denver    CO    80294
     Lowry     CO    80230
    Charlotte  FL    33949

合并数据帧

可以使用merge()函数合并两个数据帧。数据帧必须具有相同的列名称,合并发生。

在下面的例子中,我们考虑了Pima印度妇女的糖尿病数据库,可以在名称为“MASS”的库中找到。 我们根据血压值(“bp”)和体重指数(“bmi”)合并两个数据集。 在选择这两列进行合并时,这两个变量的值在两个数据集中匹配的记录被组合在一起以形成单个数据帧。参考以下代码实现 -

library(MASS)
merged.Pima <- merge(x = Pima.te, y = Pima.tr,
   by.x = c("bp", "bmi"),
   by.y = c("bp", "bmi")
)
print(merged.Pima)
nrow(merged.Pima)

当我们执行上述代码时,会产生以下结果 -

 npreg.x glu.x skin.x ped.x age.x type.x npreg.y glu.y skin.y
33.8       1   117     23 0.466    27     No       2   125     20
29.7       2    75     24 0.370    33     No       2   100     23
31.2       5   189     33 0.583    29    Yes       3   158     13
33.2       4   117     27 0.230    24     No       1    96     27
38.1       3   115     39 0.150    28     No       1   114     36
38.5       2   100     25 0.324    26     No       7   129     49
27.4       1   116     28 0.204    21     No       0   124     20
33.1       4    91     32 0.446    22     No       9   123     44
35.4       9   124     33 0.282    34     No       6   134     23
25.6       1   157     21 0.123    24     No       4    99     17
37.7       5    95     33 0.370    27     No       6   103     32
25.9       9   134     33 0.460    81     No       8   126     38
25.9       1    95     21 0.673    36     No       8   126     38
27.6       5    88     30 0.258    37     No       6   125     31
27.6      10   122     31 0.512    45     No       6   125     31
39.4       2   112     50 0.175    24     No       4   112     40
34.5       1   117     24 0.403    40    Yes       4   127     11
   ped.y age.y type.y
 0.088    31     No
 0.368    21     No
 0.295    24     No
 0.289    21     No
 0.289    21     No
 0.439    43    Yes
 0.254    36    Yes
 0.374    40     No
 0.542    29    Yes
0.294    28     No
0.324    55     No
0.162    39     No
0.162    39     No
0.565    49    Yes
0.565    49    Yes
0.236    38     No
0.598    28     No

拆分数据和重构数据

R编程最有趣的一个方面是在多个步骤中改变数据的形状以获得所需的形状。 用于执行此操作的函数称为melt()cast()

考虑使用“MASS”库中存在的数据集。

library(MASS)
print(ships)

当我们执行上述代码时,会产生以下结果 -

     type year   period   service   incidents
    A   60     60        127         0
    A   60     75         63         0
    A   65     60       1095         3
    A   65     75       1095         4
    A   70     60       1512         6
.............
.............
    A   75     75       2244         11
    B   60     60      44882         39
   B   60     75      17176         29
   B   65     60      28609         58
............
............
   C   60     60      1179          1
   C   60     75       552          1
   C   65     60       781          0
............
............

拆分数据

现在,我们将数据融合到一起,将除了类型和年份之外的所有列转为行 -

molten.ships <- melt(ships, id = c("type","year"))
print(molten.ships)

当我们执行上述代码时,会产生以下结果 -

      type year  variable  value
     A   60    period      60
     A   60    period      75
     A   65    period      60
     A   65    period      75
............
............
     B   60    period      60
    B   60    period      75
    B   65    period      60
    B   65    period      75
    B   70    period      60
...........
...........
    A   60    service    127
    A   60    service     63
    A   65    service   1095
...........
...........
    D   70    service   1208
    D   75    service      0
    D   75    service   2051
    E   60    service     45
    E   60    service      0
    E   65    service    789
...........
...........
   C   70    incidents    6
   C   70    incidents    2
   C   75    incidents    0
   C   75    incidents    1
   D   60    incidents    0
   D   60    incidents    0
...........
...........

重构数据

我们可以将拆分的数据转换为一种新形式,使用cast()函数创建每年每种类型的船的总和。

recasted.ship <- cast(molten.ships, type+year~variable,sum)
print(recasted.ship)

当我们执行上述代码时,会产生以下结果 -

     type year  period  service  incidents
    A   60    135       190      0
    A   65    135      2190      7
    A   70    135      4865     24
    A   75    135      2244     11
    B   60    135     62058     68
    B   65    135     48979    111
    B   70    135     20163     56
    B   75    135      7117     18
    C   60    135      1731      2
   C   65    135      1457      1
   C   70    135      2731      8
   C   75    135       274      1
   D   60    135       356      0
   D   65    135       480      0
   D   70    135      1557     13
   D   75    135      2051      4
   E   60    135        45      0
   E   65    135      1226     14
   E   70    135      3318     17
   E   75    135       542      1

上一篇:R语言包

下一篇:R语言CSV文件

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程