r语言 - Read an excel table



将excel表编码为R中的数据帧。我不想导出表,我想将其编码为R中的数据帧

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Period
1   0   0   0   0   0   0   0   0   0   0   0   1
0   1   0   0   0   0   0   0   0   0   0   0   2
0   0   1   0   0   0   0   0   0   0   0   0   3
0   0   0   1   0   0   0   0   0   0   0   0   4
0   0   0   0   1   0   0   0   0   0   0   0   5
0   0   0   0   0   1   0   0   0   0   0   0   6
0   0   0   0   0   0   1   0   0   0   0   0   7
0   0   0   0   0   0   0   1   0   0   0   0   8
0   0   0   0   0   0   0   0   1   0   0   0   9
0   0   0   0   0   0   0   0   0   1   0   0   10
0   0   0   0   0   0   0   0   0   0   1   0   11
0   0   0   0   0   0   0   0   0   0   0   1   12
1   0   0   0   0   0   0   0   0   0   0   0   13
0   1   0   0   0   0   0   0   0   0   0   0   14
0   0   1   0   0   0   0   0   0   0   0   0   15
0   0   0   1   0   0   0   0   0   0   0   0   16
0   0   0   0   1   0   0   0   0   0   0   0   17
0   0   0   0   0   1   0   0   0   0   0   0   18
0   0   0   0   0   0   1   0   0   0   0   0   19
0   0   0   0   0   0   0   1   0   0   0   0   20
0   0   0   0   0   0   0   0   1   0   0   0   21
0   0   0   0   0   0   0   0   0   1   0   0   22
0   0   0   0   0   0   0   0   0   0   1   0   23
0   0   0   0   0   0   0   0   0   0   0   1   24

认为你想要的是:

DF = read.table(text="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Period
1   0   0   0   0   0   0   0   0   0   0   0   1
0   1   0   0   0   0   0   0   0   0   0   0   2
0   0   1   0   0   0   0   0   0   0   0   0   3
0   0   0   1   0   0   0   0   0   0   0   0   4
0   0   0   0   1   0   0   0   0   0   0   0   5
0   0   0   0   0   1   0   0   0   0   0   0   6
0   0   0   0   0   0   1   0   0   0   0   0   7
0   0   0   0   0   0   0   1   0   0   0   0   8
0   0   0   0   0   0   0   0   1   0   0   0   9
0   0   0   0   0   0   0   0   0   1   0   0   10
0   0   0   0   0   0   0   0   0   0   1   0   11
0   0   0   0   0   0   0   0   0   0   0   1   12
1   0   0   0   0   0   0   0   0   0   0   0   13
0   1   0   0   0   0   0   0   0   0   0   0   14
0   0   1   0   0   0   0   0   0   0   0   0   15
0   0   0   1   0   0   0   0   0   0   0   0   16
0   0   0   0   1   0   0   0   0   0   0   0   17
0   0   0   0   0   1   0   0   0   0   0   0   18
0   0   0   0   0   0   1   0   0   0   0   0   19
0   0   0   0   0   0   0   1   0   0   0   0   20
0   0   0   0   0   0   0   0   1   0   0   0   21
0   0   0   0   0   0   0   0   0   1   0   0   22
0   0   0   0   0   0   0   0   0   0   1   0   23
0   0   0   0   0   0   0   0   0   0   0   1   24", 
header=T)
as.data.frame.matrix(table(1:24, factor(rep(month.abb, 2), month.abb)))
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1    1   0   0   0   0   0   0   0   0   0   0   0
2    0   1   0   0   0   0   0   0   0   0   0   0
3    0   0   1   0   0   0   0   0   0   0   0   0
4    0   0   0   1   0   0   0   0   0   0   0   0
5    0   0   0   0   1   0   0   0   0   0   0   0
6    0   0   0   0   0   1   0   0   0   0   0   0
7    0   0   0   0   0   0   1   0   0   0   0   0
8    0   0   0   0   0   0   0   1   0   0   0   0
9    0   0   0   0   0   0   0   0   1   0   0   0
10   0   0   0   0   0   0   0   0   0   1   0   0
11   0   0   0   0   0   0   0   0   0   0   1   0
12   0   0   0   0   0   0   0   0   0   0   0   1
13   1   0   0   0   0   0   0   0   0   0   0   0
14   0   1   0   0   0   0   0   0   0   0   0   0
15   0   0   1   0   0   0   0   0   0   0   0   0
16   0   0   0   1   0   0   0   0   0   0   0   0
17   0   0   0   0   1   0   0   0   0   0   0   0
18   0   0   0   0   0   1   0   0   0   0   0   0
19   0   0   0   0   0   0   1   0   0   0   0   0
20   0   0   0   0   0   0   0   1   0   0   0   0
21   0   0   0   0   0   0   0   0   1   0   0   0
22   0   0   0   0   0   0   0   0   0   1   0   0
23   0   0   0   0   0   0   0   0   0   0   1   0
24   0   0   0   0   0   0   0   0   0   0   0   1
df <-
setNames(
as.data.frame(rbind(diag(12), diag(12))),
month.abb
)
df$Period <- seq(nrow(df))
df
#>    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Period
#> 1    1   0   0   0   0   0   0   0   0   0   0   0      1
#> 2    0   1   0   0   0   0   0   0   0   0   0   0      2
#> 3    0   0   1   0   0   0   0   0   0   0   0   0      3
#> 4    0   0   0   1   0   0   0   0   0   0   0   0      4
#> 5    0   0   0   0   1   0   0   0   0   0   0   0      5
#> 6    0   0   0   0   0   1   0   0   0   0   0   0      6
#> 7    0   0   0   0   0   0   1   0   0   0   0   0      7
#> 8    0   0   0   0   0   0   0   1   0   0   0   0      8
#> 9    0   0   0   0   0   0   0   0   1   0   0   0      9
#> 10   0   0   0   0   0   0   0   0   0   1   0   0     10
#> 11   0   0   0   0   0   0   0   0   0   0   1   0     11
#> 12   0   0   0   0   0   0   0   0   0   0   0   1     12
#> 13   1   0   0   0   0   0   0   0   0   0   0   0     13
#> 14   0   1   0   0   0   0   0   0   0   0   0   0     14
#> 15   0   0   1   0   0   0   0   0   0   0   0   0     15
#> 16   0   0   0   1   0   0   0   0   0   0   0   0     16
#> 17   0   0   0   0   1   0   0   0   0   0   0   0     17
#> 18   0   0   0   0   0   1   0   0   0   0   0   0     18
#> 19   0   0   0   0   0   0   1   0   0   0   0   0     19
#> 20   0   0   0   0   0   0   0   1   0   0   0   0     20
#> 21   0   0   0   0   0   0   0   0   1   0   0   0     21
#> 22   0   0   0   0   0   0   0   0   0   1   0   0     22
#> 23   0   0   0   0   0   0   0   0   0   0   1   0     23
#> 24   0   0   0   0   0   0   0   0   0   0   0   1     24

由reprex包(v2.0.1)在2018-10-22上创建

或者使用tidyverse包,您可以制作月份和行号列表,然后向更宽的方向移动

library(dplyr, warn.conflicts = F)
library(tidyr)
df <- 
tibble(month = rep(month.abb, 2)) %>% 
mutate(Period = row_number(),
val = 1)
df
#> # A tibble: 24 × 3
#>    month Period   val
#>    <chr>  <int> <dbl>
#>  1 Jan        1     1
#>  2 Feb        2     1
#>  3 Mar        3     1
#>  4 Apr        4     1
#>  5 May        5     1
#>  6 Jun        6     1
#>  7 Jul        7     1
#>  8 Aug        8     1
#>  9 Sep        9     1
#> 10 Oct       10     1
#> # … with 14 more rows
df %>% 
pivot_wider(names_from = month,
id_cols = Period,
values_from = val,
values_fill = 0) %>% 
relocate(Period, .after = everything()) %>% 
print(width = Inf)
#> # A tibble: 24 × 13
#>      Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1     1     0     0     0     0     0     0     0     0     0     0     0
#>  2     0     1     0     0     0     0     0     0     0     0     0     0
#>  3     0     0     1     0     0     0     0     0     0     0     0     0
#>  4     0     0     0     1     0     0     0     0     0     0     0     0
#>  5     0     0     0     0     1     0     0     0     0     0     0     0
#>  6     0     0     0     0     0     1     0     0     0     0     0     0
#>  7     0     0     0     0     0     0     1     0     0     0     0     0
#>  8     0     0     0     0     0     0     0     1     0     0     0     0
#>  9     0     0     0     0     0     0     0     0     1     0     0     0
#> 10     0     0     0     0     0     0     0     0     0     1     0     0
#>    Period
#>     <int>
#>  1      1
#>  2      2
#>  3      3
#>  4      4
#>  5      5
#>  6      6
#>  7      7
#>  8      8
#>  9      9
#> 10     10
#> # … with 14 more rows

由reprex包(v2.0.1)在2018-10-22上创建

相关内容

  • 没有找到相关文章

最新更新