回答編集履歴

6

Update

2021/11/02 11:26

投稿

melian
melian

スコア20050

test CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
 
30
30
 
31
- > colnames(df)[apply(df, 2, function(c){all(c(1, 2, 9) %in% c)})]
31
+ > colnames(df)[sapply(df, function(c){all(c(1, 2, 9) %in% c)})]
32
32
 
33
33
  [1] "X3" "X9" "X10"
34
34
 
@@ -37,61 +37,3 @@
37
37
 
38
38
 
39
39
  ~~`which` を使うと良いかもしれません。~~
40
-
41
- ```r
42
-
43
- > mat <- matrix(sample.int(15, 100, TRUE), 10, 10)
44
-
45
- [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
46
-
47
- [1,] 3 2 7 9 1 3 3 12 5 13
48
-
49
- [2,] 13 12 13 10 3 8 4 3 10 9
50
-
51
- [3,] 8 11 12 14 2 5 11 8 2 9
52
-
53
- [4,] 7 11 14 13 2 14 13 8 13 10
54
-
55
- [5,] 4 10 7 3 3 2 1 6 4 14
56
-
57
- [6,] 9 2 8 15 12 12 6 9 8 6
58
-
59
- [7,] 9 1 9 11 1 12 4 4 10 7
60
-
61
- [8,] 10 10 8 5 1 2 10 6 7 4
62
-
63
- [9,] 10 5 12 15 8 15 1 13 10 14
64
-
65
- [10,] 13 11 4 10 10 7 10 7 6 1
66
-
67
-
68
-
69
- > which(mat==4, mat)
70
-
71
- row col
72
-
73
- [1,] 10 2
74
-
75
- [2,] 4 3
76
-
77
- [3,] 2 5
78
-
79
- [4,] 8 6
80
-
81
- [5,] 9 6
82
-
83
- [6,] 4 7
84
-
85
- [7,] 9 7
86
-
87
- [8,] 6 8
88
-
89
- [9,] 6 10
90
-
91
-
92
-
93
- > unique(which(mat==4, mat)[, 'col'])
94
-
95
- [1] 2 3 5 6 7 8 10
96
-
97
- ```

5

Update

2021/11/02 11:26

投稿

melian
melian

スコア20050

test CHANGED
@@ -1,6 +1,6 @@
1
1
  ```r
2
2
 
3
- > df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))
3
+ > df <- data.frame(replicate(10,sample(1:10,10,rep=TRUE)))
4
4
 
5
5
  > df
6
6
 
@@ -28,7 +28,7 @@
28
28
 
29
29
 
30
30
 
31
- > colnames(df)[apply(df, 2, function(c){ all(c(1, 2, 9) %in% c)})]
31
+ > colnames(df)[apply(df, 2, function(c){all(c(1, 2, 9) %in% c)})]
32
32
 
33
33
  [1] "X3" "X9" "X10"
34
34
 

4

Update

2021/11/02 11:23

投稿

melian
melian

スコア20050

test CHANGED
@@ -1,4 +1,42 @@
1
+ ```r
2
+
3
+ > df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))
4
+
5
+ > df
6
+
7
+ X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
8
+
9
+ 1 3 5 3 10 7 4 2 8 1 2
10
+
11
+ 2 6 9 8 8 8 4 7 1 10 1
12
+
13
+ 3 10 1 4 5 9 5 10 7 9 9
14
+
15
+ 4 2 5 4 6 6 0 0 6 4 1
16
+
17
+ 5 3 10 2 8 4 2 2 4 4 1
18
+
19
+ 6 0 10 3 4 10 3 8 2 4 10
20
+
21
+ 7 9 8 9 7 6 3 1 4 4 3
22
+
23
+ 8 2 5 9 10 6 7 4 10 8 5
24
+
25
+ 9 6 10 10 8 3 5 4 1 8 10
26
+
27
+ 10 7 4 1 6 9 8 0 2 2 6
28
+
29
+
30
+
31
+ > colnames(df)[apply(df, 2, function(c){ all(c(1, 2, 9) %in% c)})]
32
+
33
+ [1] "X3" "X9" "X10"
34
+
35
+ ```
36
+
37
+
38
+
1
- `which` を使うと良いかもしれません。
39
+ ~~`which` を使うと良いかもしれません。~~
2
40
 
3
41
  ```r
4
42
 

3

Update

2021/11/02 11:20

投稿

melian
melian

スコア20050

test CHANGED
@@ -50,4 +50,10 @@
50
50
 
51
51
  [9,] 6 10
52
52
 
53
+
54
+
55
+ > unique(which(mat==4, mat)[, 'col'])
56
+
57
+ [1] 2 3 5 6 7 8 10
58
+
53
59
  ```

2

Update

2021/11/01 08:23

投稿

melian
melian

スコア20050

test CHANGED
@@ -1,10 +1,8 @@
1
1
  `which` を使うと良いかもしれません。
2
-
3
-
4
2
 
5
3
  ```r
6
4
 
7
- > matrix(sample.int(15, 100, TRUE), 10, 10)
5
+ > mat <- matrix(sample.int(15, 100, TRUE), 10, 10)
8
6
 
9
7
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
10
8
 
@@ -28,7 +26,7 @@
28
26
 
29
27
  [10,] 13 11 4 10 10 7 10 7 6 1
30
28
 
31
- > mat <- matrix(sample.int(15, 100, TRUE), 10, 10)
29
+
32
30
 
33
31
  > which(mat==4, mat)
34
32
 

1

Update

2021/11/01 08:14

投稿

melian
melian

スコア20050

test CHANGED
@@ -1,3 +1,7 @@
1
+ `which` を使うと良いかもしれません。
2
+
3
+
4
+
1
5
  ```r
2
6
 
3
7
  > matrix(sample.int(15, 100, TRUE), 10, 10)