質問編集履歴

8

追記

2021/10/07 06:29

投稿

Mayu___
Mayu___

スコア4

test CHANGED
@@ -1 +1 @@
1
- R パネルデータの整理にあたり、観測期間(年次データ)のすべてが揃った個体のみを、新しいデータフレームに保存したいです。
1
+ tuiR パネルデータの整理にあたり、観測期間(年次データ)のすべてが揃った個体のみを、新しいデータフレームに保存したいです。
test CHANGED
@@ -76,7 +76,7 @@
76
76
 
77
77
 
78
78
 
79
- for(coname in unique(sorted_data$"企業名")){
79
+ for(coname in unique(sorted_data$"name")){
80
80
 
81
81
  if(all(y %in% sorted_data[sorted_data$"name"==coname,"year"])){
82
82
 

7

文法の修正

2021/10/07 06:29

投稿

Mayu___
Mayu___

スコア4

test CHANGED
File without changes
test CHANGED
@@ -67,12 +67,6 @@
67
67
  ```ここに言語名を入力
68
68
 
69
69
  sorted_data<-arrange(merge_ALLdata,name,year)
70
-
71
-
72
-
73
-
74
-
75
- df1 <- read.table("test.txt", header=T, sep="\t", stringsAsFactors=F)
76
70
 
77
71
 
78
72
 

6

質問に対する追記

2021/10/07 06:27

投稿

Mayu___
Mayu___

スコア4

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,11 @@
4
4
 
5
5
 
6
6
 
7
+ 使用ソフト:R
8
+
7
- Rを使って、以下データ中から1999年∼2001年のデータが'全て'揃っている企業のみデータフレームに保存したいです(データフレーム ...df1)
9
+ いくつかcsvファイルのデータを結合、ソートデータフレーム(sorted_data)を作成ました。
10
+
11
+ sorted_dataは多少簡略化していますが、以下のようなものになります。
8
12
 
9
13
 
10
14
 
@@ -36,7 +40,7 @@
36
40
 
37
41
 
38
42
 
39
- 目標は以下の形になります。
43
+ この中から1999年∼2001年のデータが'全て'揃っている企業のみを新しいデータフレームに保存したいです。目標は以下の形になります。
40
44
 
41
45
  name year ...
42
46
 
@@ -56,11 +60,17 @@
56
60
 
57
61
 
58
62
 
59
- そこで、以下のソースコードを試しました。
63
+ そこで、以下のソースコードを試しました。(一行目のコードで結合されたデータをソートし、"sorted_data"というデータフレームを作成しております)
60
64
 
61
65
  ### 該当のソースコード
62
66
 
63
67
  ```ここに言語名を入力
68
+
69
+ sorted_data<-arrange(merge_ALLdata,name,year)
70
+
71
+
72
+
73
+
64
74
 
65
75
  df1 <- read.table("test.txt", header=T, sep="\t", stringsAsFactors=F)
66
76
 
@@ -72,9 +82,9 @@
72
82
 
73
83
 
74
84
 
75
- for(coname in unique(df1$"企業名")){
85
+ for(coname in unique(sorted_data$"企業名")){
76
86
 
77
- if(all(y %in% df1[df1$"企業名"==coname,"年度"])){
87
+ if(all(y %in% sorted_data[sorted_data$"name"==coname,"year"])){
78
88
 
79
89
  print(paste("yes", coname))
80
90
 
@@ -88,7 +98,7 @@
88
98
 
89
99
  }
90
100
 
91
- newdf <- df1[df1[,1]%in%hit,]
101
+ newdf <- sorted_data[sorted_data[,1]%in%hit,]
92
102
 
93
103
  ```
94
104
 

5

質問に対する追記

2021/10/07 06:25

投稿

Mayu___
Mayu___

スコア4

test CHANGED
File without changes
test CHANGED
@@ -116,8 +116,68 @@
116
116
 
117
117
  言葉足らずな部分もあるかと思いますが、ご教示お願い致します。
118
118
 
119
+ ```
119
120
 
120
121
 
121
122
 
123
+ ### 追記
124
+
125
+
126
+
127
+ ```ここに言語名を入力
128
+
129
+ 実行環境は以下の通りになります。(sessionInfo()結果)
130
+
131
+
132
+
133
+ R version 4.0.0 (2020-04-24)
134
+
135
+ Platform: x86_64-w64-mingw32/x64 (64-bit)
136
+
137
+ Running under: Windows 10 x64 (build 19043)
138
+
139
+
140
+
141
+ Matrix products: default
142
+
143
+
144
+
145
+ locale:
146
+
147
+ [1] LC_COLLATE=Japanese_Japan.932 LC_CTYPE=Japanese_Japan.932 LC_MONETARY=Japanese_Japan.932
148
+
149
+ [4] LC_NUMERIC=C LC_TIME=Japanese_Japan.932
150
+
151
+
152
+
153
+ attached base packages:
154
+
155
+ [1] stats graphics grDevices utils datasets methods base
156
+
157
+
158
+
159
+ other attached packages:
160
+
161
+ [1] forcats_0.5.0 stringr_1.4.0 dplyr_1.0.0 purrr_0.3.4 readr_1.3.1 tidyr_1.1.0 tibble_3.0.1
162
+
163
+ [8] ggplot2_3.3.0 tidyverse_1.3.0
164
+
165
+
166
+
167
+ loaded via a namespace (and not attached):
168
+
169
+ [1] Rcpp_1.0.4.6 cellranger_1.1.0 pillar_1.4.4 compiler_4.0.0 dbplyr_1.4.4 tools_4.0.0 lubridate_1.7.9
170
+
171
+ [8] jsonlite_1.6.1 lifecycle_0.2.0 nlme_3.1-147 gtable_0.3.0 lattice_0.20-41 pkgconfig_2.0.3 rlang_0.4.6
172
+
173
+ [15] reprex_0.3.0 cli_2.0.2 DBI_1.1.0 rstudioapi_0.11 haven_2.3.1 withr_2.2.0 xml2_1.3.2
174
+
175
+ [22] httr_1.4.1 fs_1.4.1 generics_0.0.2 vctrs_0.3.0 hms_0.5.3 grid_4.0.0 tidyselect_1.1.0
176
+
177
+ [29] glue_1.4.1 R6_2.4.1 fansi_0.4.1 readxl_1.3.1 modelr_0.1.8 blob_1.2.1 magrittr_1.5
178
+
179
+ [36] backports_1.1.6 scales_1.1.1 ellipsis_0.3.1 rvest_0.3.5 assertthat_0.2.1 colorspace_1.4-1 stringi_1.4.6
180
+
181
+ [43] munsell_0.5.0 broom_0.5.6 crayon_1.3.4
122
182
 
123
183
  ```

4

タグの追加

2021/10/06 07:02

投稿

Mayu___
Mayu___

スコア4

test CHANGED
File without changes
test CHANGED
File without changes

3

追記

2021/10/06 03:54

投稿

Mayu___
Mayu___

スコア4

test CHANGED
File without changes
test CHANGED
@@ -112,11 +112,9 @@
112
112
 
113
113
  おそらく最初の条件式の書き方に何か誤りがあるのではないかと思います。
114
114
 
115
+ 上でお示ししたコードにこだわっているわけではないので、他によいやり方があれば教えていただけると嬉しいです。
116
+
115
117
  言葉足らずな部分もあるかと思いますが、ご教示お願い致します。
116
-
117
-
118
-
119
- また上でお示ししたコードにこだわっているわけではないので、他によいやり方があれば教えていただけると嬉しいです。
120
118
 
121
119
 
122
120
 

2

追記

2021/10/06 00:51

投稿

Mayu___
Mayu___

スコア4

test CHANGED
File without changes
test CHANGED
@@ -116,6 +116,10 @@
116
116
 
117
117
 
118
118
 
119
+ また上でお示ししたコードにこだわっているわけではないので、他によいやり方があれば教えていただけると嬉しいです。
120
+
121
+
122
+
119
123
 
120
124
 
121
125
  ```

1

文法の修正

2021/10/06 00:46

投稿

Mayu___
Mayu___

スコア4

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- Rを使って、以下のデータの中から1999年∼2001年のデータが'全て'揃っている企業を抽出したいです。(データフレーム ...df1)
7
+ Rを使って、以下のデータの中から1999年∼2001年のデータが'全て'揃っている企業のみいデータフレームに保存したいです。(データフレーム ...df1)
8
8
 
9
9
 
10
10