R语言数据重塑

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 ")

# 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 ")

# 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 ")

# 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
2 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
6 Charlotte    FL   33949

合并数据帧

我们可以通过使用 merge()函数合并两个数据帧。该数据帧必须在其上合并发生相同的列名。

在下面的例子中,我们考虑对皮马印第安人妇女的糖尿病在可用的数据集库名称 "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)

当我们上面的代码执行时,它产生以下结果:

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

熔化和转换

R语言编程的最有趣的地方是关于改变多个步骤中的数据的形状来获得所希望的形状。用来做这种函数被称为 melt() 和 cast()。

我们认为数据集被称为 ships 出现在库被称为 "MASS".

library(MASS)
print(ships)

当我们上面的代码执行时,它产生以下结果:

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

融化数据

现在,我们融化数据需要组织其转换类型(type), 并且 year 到多行以外的所有列。

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

当我们上面的代码执行时,它产生以下结果:

    type year  variable value
      60    period    60
      60    period    75
      65    period    60
      65    period    75
............
............
      60    period    60
10      60    period    75
11      65    period    60
12      65    period    75
13      70    period    60
...........
...........
41      60   service   127
42      60   service    63
43      65   service  1095
...........
...........
70      70   service  1208
71      75   service     0
72      75   service  2051
73      60   service    45
74      60   service     0
75      65   service   789
...........
...........
101     70 incidents     6
102     70 incidents     2
103     75 incidents     0
104     75 incidents     1
105     60 incidents     0
106     60 incidents     0
...........
...........

转换数据

我们可以转化数据转换成在创建每种类型的 ships 每年的汇总的新形式。它是通过使用 case()函数。

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

当我们上面的代码执行时,它产生以下结果:

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

原文地址:https://www.cnblogs.com/amengduo/p/9587020.html