keep the observation belonging to the cluster in R - r

Simple example with iris dataset. I must use apcluster library
library("apcluster")
#use dist() create a negative SimilarityMatrix
sim<-negDistMat(iris[,1:4],r=2)
#run the clusteralgorythm and create apclustert object apiris1
apiris1<-apcluster(sim,details=T)
apiris1=apclusterK(sim,details=T,K=2,verbose=T)
and after, i see the number of cluster and obzervation in it
Cluster 1, exemplar 8:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50 58 99
Cluster 2, exemplar 124:
51 52 53 54 55 56 57 59 60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
88 89 90 91 92 93 94 95 96 97 98 100 101 102 103 104
105 106 107 108 109 110 111 112 113 114 115 116 117 118
119 120 121 122 123 124 125 126 127 128 129 130 131 132
133 134 135 136 137 138 139 140 141 142 143 144 145 146
147 148 149 150
How to keep the observation belonging to the cluster in R.
To make my post more clear, on output I expect such a table
n Sepal.Length Sepal.Width Petal.Length Petal.Width Species Save.cluster
1 1 5.1 3.5 1.4 0.2 setosa 1
2 2 4.9 3.0 1.4 0.2 setosa 1
3 3 4.7 3.2 1.3 0.2 setosa 1
4 4 4.6 3.1 1.5 0.2 setosa 1
5 5 5.0 3.6 1.4 0.2 setosa 1
6 6 5.4 3.9 1.7 0.4 setosa 1
7 7 4.6 3.4 1.4 0.3 setosa 1
8 8 5.0 3.4 1.5 0.2 setosa 1
9 9 4.4 2.9 1.4 0.2 setosa 1
10 10 4.9 3.1 1.5 0.1 setosa 1
11 51 7.0 3.2 4.7 1.4 versicolor 2
12 52 6.4 3.2 4.5 1.5 versicolor 2
13 53 6.9 3.1 4.9 1.5 versicolor 2
14 54 5.5 2.3 4.0 1.3 versicolor 2
15 55 6.5 2.8 4.6 1.5 versicolor 2

The cluster indices are stored in apiris1#clusters. You can make a data.frame like the one you are requesting like this:
iris1 = iris
iris1$Save.cluster = 0
for(i in 1:length(apiris1#clusters)) {
iris1$Save.cluster[apiris1#clusters[[i]]] = i }
head(iris1)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Save.cluster
1 5.1 3.5 1.4 0.2 setosa 1
2 4.9 3.0 1.4 0.2 setosa 1
3 4.7 3.2 1.3 0.2 setosa 1
4 4.6 3.1 1.5 0.2 setosa 1
5 5.0 3.6 1.4 0.2 setosa 1
6 5.4 3.9 1.7 0.4 setosa 1

Related

using R to impute missing data with the mean of the available data if there was less than 10% missing data

How to use R to impute missing data with the mean of the available data across rows if there was less than 10% missing data across rows?
I would use {dplyr} and {naniar}
dplyr::mutate_if(iris2, ~ mean(is.na(.x)) > .1, naniar::impute_mean)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.100000 3.500000 1.4 0.2 setosa
#> 2 4.900000 3.072308 1.4 0.2 setosa
#> 3 4.700000 3.200000 1.3 0.2 setosa
#> 4 5.813077 3.100000 1.5 0.2 setosa
#> 5 5.000000 3.600000 1.4 0.2 setosa
#> 6 5.400000 3.900000 1.7 0.4 setosa
#> 7 4.600000 3.400000 1.4 0.3 setosa
#> 8 5.000000 3.400000 1.5 0.2 setosa
#> 9 4.400000 2.900000 1.4 0.2 setosa
#> 10 4.900000 3.100000 1.5 0.1 setosa
#> 11 5.400000 3.700000 1.5 0.2 setosa
#> 12 4.800000 3.400000 1.6 0.2 setosa
#> 13 4.800000 3.000000 1.4 0.1 setosa
#> 14 5.813077 3.000000 1.1 0.1 setosa
#> 15 5.800000 4.000000 1.2 0.2 setosa
#> 16 5.700000 4.400000 1.5 0.4 setosa
#> 17 5.400000 3.900000 1.3 0.4 setosa
#> 18 5.100000 3.500000 1.4 0.3 setosa
#> 19 5.700000 3.800000 1.7 0.3 setosa
#> 20 5.100000 3.800000 1.5 0.3 setosa
#> 21 5.400000 3.400000 1.7 0.2 setosa
#> 22 5.100000 3.700000 1.5 0.4 setosa
#> 23 4.600000 3.600000 1.0 0.2 setosa
#> 24 5.100000 3.300000 1.7 0.5 setosa
#> 25 4.800000 3.400000 1.9 0.2 setosa
#> 26 5.000000 3.000000 1.6 0.2 setosa
#> 27 5.000000 3.400000 1.6 0.4 setosa
#> 28 5.813077 3.500000 1.5 0.2 setosa
#> 29 5.200000 3.400000 1.4 0.2 setosa
#> 30 4.700000 3.200000 1.6 0.2 setosa
#> 31 4.800000 3.100000 1.6 0.2 setosa
#> 32 5.400000 3.400000 1.5 0.4 setosa
#> 33 5.200000 4.100000 1.5 0.1 setosa
#> 34 5.500000 4.200000 1.4 0.2 setosa
#> 35 4.900000 3.100000 1.5 0.2 setosa
#> 36 5.000000 3.200000 1.2 0.2 setosa
#> 37 5.500000 3.500000 1.3 0.2 setosa
#> 38 4.900000 3.600000 1.4 0.1 setosa
#> 39 4.400000 3.000000 1.3 0.2 setosa
#> 40 5.813077 3.400000 1.5 0.2 setosa
#> 41 5.000000 3.072308 1.3 0.3 setosa
#> 42 4.500000 3.072308 1.3 0.3 setosa
#> 43 4.400000 3.072308 1.3 0.2 setosa
#> 44 5.000000 3.500000 1.6 0.6 setosa
#> 45 5.100000 3.800000 1.9 0.4 setosa
#> 46 4.800000 3.000000 1.4 0.3 setosa
#> 47 5.100000 3.800000 1.6 0.2 setosa
#> 48 4.600000 3.072308 1.4 0.2 setosa
#> 49 5.300000 3.072308 1.5 0.2 setosa
#> 50 5.000000 3.300000 1.4 0.2 setosa
#> 51 7.000000 3.072308 4.7 1.4 versicolor
#> 52 6.400000 3.200000 4.5 1.5 versicolor
#> 53 6.900000 3.100000 4.9 1.5 versicolor
#> 54 5.500000 2.300000 4.0 1.3 versicolor
#> 55 6.500000 2.800000 4.6 1.5 versicolor
#> 56 5.700000 2.800000 4.5 1.3 versicolor
#> 57 6.300000 3.300000 4.7 1.6 versicolor
#> 58 4.900000 2.400000 3.3 1.0 versicolor
#> 59 6.600000 2.900000 4.6 1.3 versicolor
#> 60 5.200000 2.700000 3.9 1.4 versicolor
#> 61 5.000000 2.000000 3.5 1.0 versicolor
#> 62 5.813077 3.000000 4.2 1.5 versicolor
#> 63 6.000000 2.200000 4.0 1.0 versicolor
#> 64 6.100000 2.900000 4.7 1.4 versicolor
#> 65 5.600000 2.900000 3.6 1.3 versicolor
#> 66 6.700000 3.072308 4.4 1.4 versicolor
#> 67 5.600000 3.000000 4.5 1.5 versicolor
#> 68 5.800000 2.700000 4.1 1.0 versicolor
#> 69 6.200000 2.200000 4.5 1.5 versicolor
#> 70 5.813077 2.500000 3.9 1.1 versicolor
#> 71 5.900000 3.200000 4.8 1.8 versicolor
#> 72 6.100000 3.072308 4.0 1.3 versicolor
#> 73 6.300000 2.500000 4.9 1.5 versicolor
#> 74 6.100000 2.800000 4.7 1.2 versicolor
#> 75 6.400000 2.900000 4.3 1.3 versicolor
#> 76 6.600000 3.000000 4.4 1.4 versicolor
#> 77 6.800000 2.800000 4.8 1.4 versicolor
#> 78 6.700000 3.000000 5.0 1.7 versicolor
#> 79 5.813077 3.072308 4.5 1.5 versicolor
#> 80 5.813077 2.600000 3.5 1.0 versicolor
#> 81 5.500000 2.400000 3.8 1.1 versicolor
#> 82 5.500000 2.400000 3.7 1.0 versicolor
#> 83 5.800000 2.700000 3.9 1.2 versicolor
#> 84 6.000000 2.700000 5.1 1.6 versicolor
#> 85 5.400000 3.000000 4.5 1.5 versicolor
#> 86 6.000000 3.400000 4.5 1.6 versicolor
#> 87 6.700000 3.072308 4.7 1.5 versicolor
#> 88 6.300000 2.300000 4.4 1.3 versicolor
#> 89 5.600000 3.000000 4.1 1.3 versicolor
#> 90 5.813077 2.500000 4.0 1.3 versicolor
#> 91 5.500000 2.600000 4.4 1.2 versicolor
#> 92 6.100000 3.000000 4.6 1.4 versicolor
#> 93 5.800000 3.072308 4.0 1.2 versicolor
#> 94 5.000000 2.300000 3.3 1.0 versicolor
#> 95 5.600000 2.700000 4.2 1.3 versicolor
#> 96 5.700000 3.000000 4.2 1.2 versicolor
#> 97 5.700000 2.900000 4.2 1.3 versicolor
#> 98 5.813077 2.900000 4.3 1.3 versicolor
#> 99 5.100000 2.500000 3.0 1.1 versicolor
#> 100 5.700000 2.800000 4.1 1.3 versicolor
#> 101 5.813077 3.300000 6.0 2.5 virginica
#> 102 5.800000 3.072308 5.1 1.9 virginica
#> 103 5.813077 3.000000 5.9 2.1 virginica
#> 104 6.300000 2.900000 5.6 1.8 virginica
#> 105 6.500000 3.000000 5.8 2.2 virginica
#> 106 7.600000 3.000000 6.6 2.1 virginica
#> 107 4.900000 2.500000 4.5 1.7 virginica
#> 108 7.300000 3.072308 6.3 1.8 virginica
#> 109 6.700000 2.500000 5.8 1.8 virginica
#> 110 7.200000 3.600000 6.1 2.5 virginica
#> 111 5.813077 3.200000 5.1 2.0 virginica
#> 112 6.400000 2.700000 5.3 1.9 virginica
#> 113 6.800000 3.000000 5.5 2.1 virginica
#> 114 5.700000 2.500000 5.0 2.0 virginica
#> 115 5.800000 3.072308 5.1 2.4 virginica
#> 116 5.813077 3.200000 5.3 2.3 virginica
#> 117 6.500000 3.072308 5.5 1.8 virginica
#> 118 7.700000 3.800000 6.7 2.2 virginica
#> 119 7.700000 2.600000 6.9 2.3 virginica
#> 120 6.000000 2.200000 5.0 1.5 virginica
#> 121 6.900000 3.200000 5.7 2.3 virginica
#> 122 5.600000 3.072308 4.9 2.0 virginica
#> 123 7.700000 3.072308 6.7 2.0 virginica
#> 124 6.300000 2.700000 4.9 1.8 virginica
#> 125 6.700000 3.300000 5.7 2.1 virginica
#> 126 5.813077 3.200000 6.0 1.8 virginica
#> 127 6.200000 2.800000 4.8 1.8 virginica
#> 128 6.100000 3.000000 4.9 1.8 virginica
#> 129 6.400000 2.800000 5.6 2.1 virginica
#> 130 7.200000 3.000000 5.8 1.6 virginica
#> 131 7.400000 3.072308 6.1 1.9 virginica
#> 132 5.813077 3.800000 6.4 2.0 virginica
#> 133 5.813077 3.072308 5.6 2.2 virginica
#> 134 6.300000 2.800000 5.1 1.5 virginica
#> 135 6.100000 2.600000 5.6 1.4 virginica
#> 136 7.700000 3.000000 6.1 2.3 virginica
#> 137 5.813077 3.400000 5.6 2.4 virginica
#> 138 6.400000 3.100000 5.5 1.8 virginica
#> 139 6.000000 3.000000 4.8 1.8 virginica
#> 140 6.900000 3.100000 5.4 2.1 virginica
#> 141 6.700000 3.100000 5.6 2.4 virginica
#> 142 6.900000 3.100000 5.1 2.3 virginica
#> 143 5.813077 2.700000 5.1 1.9 virginica
#> 144 5.813077 3.200000 5.9 2.3 virginica
#> 145 6.700000 3.300000 5.7 2.5 virginica
#> 146 6.700000 3.000000 5.2 2.3 virginica
#> 147 6.300000 2.500000 5.0 1.9 virginica
#> 148 6.500000 3.000000 5.2 2.0 virginica
#> 149 6.200000 3.400000 5.4 2.3 virginica
#> 150 5.900000 3.000000 5.1 1.8 virginica
library(tidyverse)
tribble(
~a, ~b, ~c,
1, 1, 1,
3, 4, 2,
1, 1, 1,
3, 4, 2,
1, 1, 1,
3, 4, 2,
1, 1, 1,
3, NA, 2,
NA, NA, 1,
NA, NA, NA,
1, 4, 2
) |>
mutate(across(
everything(),
~ if_else(is.na(.) & mean(is.na(.)) < 0.1,
mean(., na.rm = TRUE), .
)
))
#> # A tibble: 11 × 3
#> a b c
#> <dbl> <dbl> <dbl>
#> 1 1 1 1
#> 2 3 4 2
#> 3 1 1 1
#> 4 3 4 2
#> 5 1 1 1
#> 6 3 4 2
#> 7 1 1 1
#> 8 3 NA 2
#> 9 NA NA 1
#> 10 NA NA 1.5
#> 11 1 4 2
Created on 2022-08-09 by the reprex package (v2.0.1)
Here’s a base R solution, using made-up data:
# compute % missing by row
row_pct_na <- rowSums(is.na(fakedata)) / ncol(fakedata)
# replace missings conditionally
for (i in seq_along(fakedata)) {
fakedata[[i]][is.na(fakedata[[i]]) & row_pct_na < .1] <- mean(fakedata[[i]], na.rm = TRUE)
}
Check results: rows that had <10% missing should now have 0% missing, but rows that had >=10% missing should still have same percent missing.
new_row_pct_na <- rowSums(is.na(fakedata)) / ncol(fakedata)
# before imputation
row_pct_na
# [1] 0.07692308 0.03846154 0.03846154 0.11538462 0.03846154 0.07692308
# [7] 0.00000000 0.00000000 0.00000000 0.11538462
# after imputation
new_row_pct_na
# [1] 0.0000000 0.0000000 0.0000000 0.1153846 0.0000000 0.0000000 0.0000000
# [8] 0.0000000 0.0000000 0.1153846
Data prep:
set.seed(1)
fakedata <- list()
for (letter in letters) {
fakedata[[letter]] <- rnorm(10)
fakedata[[letter]][runif(10) > .95] <- NA
}
fakedata <- as.data.frame(fakedata)

How to split a dataframe based on column class

Let's take the iris dataset for example.
I want to create two dataframes from this one. This first one would have the contiuous variables, and the second one the discrete ones.
What I do first is create a list with the category of the column
iris <- iris
a <- lapply(iris, class)
Then I create two empty lists and store the index of both discrete and continuous columns.
cont <- list()
disc <- list()
for (i in 1:length(a)){
if (a[[i]][1] == "numeric")
cont <- append(cont, i)
else
disc <- append(disc, i)
}
But I do not know how to split based on this lists.
Try the following code:
classes_iris <- sapply(iris, class)
split.default(iris, classes_iris)
Output:
$factor
Species
1 setosa
2 setosa
3 setosa
4 setosa
5 setosa
6 setosa
7 setosa
8 setosa
9 setosa
10 setosa
11 setosa
12 setosa
13 setosa
14 setosa
15 setosa
16 setosa
17 setosa
18 setosa
19 setosa
20 setosa
21 setosa
22 setosa
23 setosa
24 setosa
25 setosa
26 setosa
27 setosa
28 setosa
29 setosa
30 setosa
31 setosa
32 setosa
33 setosa
34 setosa
35 setosa
36 setosa
37 setosa
38 setosa
39 setosa
40 setosa
41 setosa
42 setosa
43 setosa
44 setosa
45 setosa
46 setosa
47 setosa
48 setosa
49 setosa
50 setosa
51 versicolor
52 versicolor
53 versicolor
54 versicolor
55 versicolor
56 versicolor
57 versicolor
58 versicolor
59 versicolor
60 versicolor
61 versicolor
62 versicolor
63 versicolor
64 versicolor
65 versicolor
66 versicolor
67 versicolor
68 versicolor
69 versicolor
70 versicolor
71 versicolor
72 versicolor
73 versicolor
74 versicolor
75 versicolor
76 versicolor
77 versicolor
78 versicolor
79 versicolor
80 versicolor
81 versicolor
82 versicolor
83 versicolor
84 versicolor
85 versicolor
86 versicolor
87 versicolor
88 versicolor
89 versicolor
90 versicolor
91 versicolor
92 versicolor
93 versicolor
94 versicolor
95 versicolor
96 versicolor
97 versicolor
98 versicolor
99 versicolor
100 versicolor
101 virginica
102 virginica
103 virginica
104 virginica
105 virginica
106 virginica
107 virginica
108 virginica
109 virginica
110 virginica
111 virginica
112 virginica
113 virginica
114 virginica
115 virginica
116 virginica
117 virginica
118 virginica
119 virginica
120 virginica
121 virginica
122 virginica
123 virginica
124 virginica
125 virginica
126 virginica
127 virginica
128 virginica
129 virginica
130 virginica
131 virginica
132 virginica
133 virginica
134 virginica
135 virginica
136 virginica
137 virginica
138 virginica
139 virginica
140 virginica
141 virginica
142 virginica
143 virginica
144 virginica
145 virginica
146 virginica
147 virginica
148 virginica
149 virginica
150 virginica
$numeric
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
7 4.6 3.4 1.4 0.3
8 5.0 3.4 1.5 0.2
9 4.4 2.9 1.4 0.2
10 4.9 3.1 1.5 0.1
11 5.4 3.7 1.5 0.2
12 4.8 3.4 1.6 0.2
13 4.8 3.0 1.4 0.1
14 4.3 3.0 1.1 0.1
15 5.8 4.0 1.2 0.2
16 5.7 4.4 1.5 0.4
17 5.4 3.9 1.3 0.4
18 5.1 3.5 1.4 0.3
19 5.7 3.8 1.7 0.3
20 5.1 3.8 1.5 0.3
21 5.4 3.4 1.7 0.2
22 5.1 3.7 1.5 0.4
23 4.6 3.6 1.0 0.2
24 5.1 3.3 1.7 0.5
25 4.8 3.4 1.9 0.2
26 5.0 3.0 1.6 0.2
27 5.0 3.4 1.6 0.4
28 5.2 3.5 1.5 0.2
29 5.2 3.4 1.4 0.2
30 4.7 3.2 1.6 0.2
31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.3
145 6.7 3.3 5.7 2.5
146 6.7 3.0 5.2 2.3
147 6.3 2.5 5.0 1.9
148 6.5 3.0 5.2 2.0
149 6.2 3.4 5.4 2.3
150 5.9 3.0 5.1 1.8
As you can see it is split based on the classes factor and numeric.

Why ifelse with character statement is not working?

I am having problem today to understand how functions works.
This is my code:
my_fun<- function(x){
ifelse(as.character(x) == 'Species',
a<- iris %>% select(x),
a<- iris*2)
a
}
my_fun(x = Species)
Why is it not working?
There are three changes to be made,
converting to character would be with deparse/substitute
if/else would be appropriate as ifelse requires all the arguments to be of same length
iris * 2 as an else option wouldn't work as the dataset includes a factor column as well. So, we need to multiply only those numeric columns
my_fun <- function(x) {
x <- deparse(substitute(x))
if(x == 'Species') {
iris %>%
select(all_of(x))
} else {
iris %>%
mutate(across(where(is.numeric), ~ .* 2))
}
}
-testing
my_fun(Species)
# Species
#1 setosa
#2 setosa
#3 setosa
#4 setosa
#5 setosa
#6 setosa
#...
and if we pass another input
my_fun(hello)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 10.2 7.0 2.8 0.4 setosa
#2 9.8 6.0 2.8 0.4 setosa
#3 9.4 6.4 2.6 0.4 setosa
#4 9.2 6.2 3.0 0.4 setosa
#5 10.0 7.2 2.8 0.4 setosa
#6 10.8 7.8 3.4 0.8 setosa
# ...
library(tidyverse)
my_fun <- function(x) {
if(x == "Species") {
iris %>% select(x)
} else {
iris * 2
}
}
my_fun("Species")
Why?
You have an error because Species when evaluated is not found since it was not a defined variable.
One can use Non Standard Evaluation (NSE) feature with deparse and substitute see #akrun answer. But maybe you want to call your function with a character like that my_fun("Species").
The other mistake is that ifelse is a vectorized version of if/else and here you just want to test one value (x).
If you would like to ifelse anyway, here is a variation base on answers by #akrun and #pietrodito
my_fun <- function(x) {
x <- deparse(substitute(x))
ifelse(x == "Species",
a <- list(iris %>% select(x)),
a <- list(iris * 2)
)
a[[1]]
}
which gives
> my_fun(Species)
Species
1 setosa
2 setosa
3 setosa
4 setosa
5 setosa
6 setosa
7 setosa
8 setosa
9 setosa
10 setosa
11 setosa
12 setosa
13 setosa
14 setosa
15 setosa
16 setosa
17 setosa
18 setosa
19 setosa
20 setosa
21 setosa
22 setosa
23 setosa
24 setosa
25 setosa
26 setosa
27 setosa
28 setosa
29 setosa
30 setosa
31 setosa
32 setosa
33 setosa
34 setosa
35 setosa
36 setosa
37 setosa
38 setosa
39 setosa
40 setosa
41 setosa
42 setosa
43 setosa
44 setosa
45 setosa
46 setosa
47 setosa
48 setosa
49 setosa
50 setosa
51 versicolor
52 versicolor
53 versicolor
54 versicolor
55 versicolor
56 versicolor
57 versicolor
58 versicolor
59 versicolor
60 versicolor
61 versicolor
62 versicolor
63 versicolor
64 versicolor
65 versicolor
66 versicolor
67 versicolor
68 versicolor
69 versicolor
70 versicolor
71 versicolor
72 versicolor
73 versicolor
74 versicolor
75 versicolor
76 versicolor
77 versicolor
78 versicolor
79 versicolor
80 versicolor
81 versicolor
82 versicolor
83 versicolor
84 versicolor
85 versicolor
86 versicolor
87 versicolor
88 versicolor
89 versicolor
90 versicolor
91 versicolor
92 versicolor
93 versicolor
94 versicolor
95 versicolor
96 versicolor
97 versicolor
98 versicolor
99 versicolor
100 versicolor
101 virginica
102 virginica
103 virginica
104 virginica
105 virginica
106 virginica
107 virginica
108 virginica
109 virginica
110 virginica
111 virginica
112 virginica
113 virginica
114 virginica
115 virginica
116 virginica
117 virginica
118 virginica
119 virginica
120 virginica
121 virginica
122 virginica
123 virginica
124 virginica
125 virginica
126 virginica
127 virginica
128 virginica
129 virginica
130 virginica
131 virginica
132 virginica
133 virginica
134 virginica
135 virginica
136 virginica
137 virginica
138 virginica
139 virginica
140 virginica
141 virginica
142 virginica
143 virginica
144 virginica
145 virginica
146 virginica
147 virginica
148 virginica
149 virginica
150 virginica

Reformat data frame into a single row

I want to format my data frame into a single row.
So I have this
RT 200 201 202 203 204 205
2 2.5 3.5 4.5 5.5 6.5 7.5
3 2.6 3.6 4.6 5.6 6.6 7.6
4 2.7 3.7 4.7 5.7 6.7 7.7
And I want this:
m/z 200 201 202 203 204 205 200 201 202 203 204 205 200 201 202 203 204 205
RT 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
Sa 2.5 3.5 4.5 5.5 6.5 7.5 2.6 3.6 4.6 5.6 6.6 7.6 2.7 3.7 4.7 5.7 6.7 7.7
Can anyone provide me code for this?
Note: I want to add row names "m/z" and "Sa" to the rows instead of leaving it blank.

Setting defaults by ggproto when extending ggplot2

I ran the R Code posted in http://ggplot2.tidyverse.org/articles/extending-ggplot2.html#picking-defaults
and the following is the modified code that added some print() to show the values of variables in each step, my questions are marked as comments in the code:
StatDensityCommon <- ggproto("StatDensityCommon", Stat, required_aes = "x",
setup_params = function(data, params) {
print("PARAMS BEFORE:")
print(params)
if(!is.null(params$bandwidth))
return(params)
print("DATA: ")
print(data)
#1. When and how does the data being modified and the "group" field added?
xs <- split(data$x, data$group)
print("XS: ")
print(xs)
bws <- vapply(xs, bw.nrd0, numeric(1))
print("BWS: ")
print(bws)
bw <- mean(bws)
print("BW: ")
print(bw)
message("Picking bandwidth of ", signif(bw, 3))
params$bandwidth <- bw
print("PARAMS AFTER: ")
print(params)
params
},
compute_group = function(data, scales, bandwidth = 1) {
#2. how does the bandwidth computed in setup_params passed into compute_group
#even if the bandwidth has already been set to 1 in the arguments?
d <- density(data$x, bw = bandwidth)
data.frame(x = d$x, y = d$y)
}
)
stat_density_common <- function(mapping = NULL, data = NULL, geom = "line", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, bandwidth = NULL, ...){
layer(stat = StatDensityCommon, data = data, mapping = mapping, geom = geom, position = position, show.legend = show.legend, inherit.aes = inherit.aes, params = list(bandwidth = bandwidth, na.rm = na.rm, ...))
}
ggplot(mpg, aes(displ, colour = drv)) + stat_density_common()
The following are the outputs except the plot:
[1] "PARAMS BEFORE:"
$bandwidth
NULL
$na.rm
[1] FALSE
[1] "DATA: "
x colour PANEL group
1 1.8 f 1 2
2 1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 1 1
88 4.6 4 1 1
89 5.4 4 1 1
90 5.4 4 1 1
91 3.8 r 1 3
92 3.8 r 1 3
93 4.0 r 1 3
94 4.0 r 1 3
95 4.6 r 1 3
96 4.6 r 1 3
97 4.6 r 1 3
98 4.6 r 1 3
99 5.4 r 1 3
100 1.6 f 1 2
101 1.6 f 1 2
102 1.6 f 1 2
103 1.6 f 1 2
104 1.6 f 1 2
105 1.8 f 1 2
106 1.8 f 1 2
107 1.8 f 1 2
108 2.0 f 1 2
109 2.4 f 1 2
110 2.4 f 1 2
111 2.4 f 1 2
112 2.4 f 1 2
113 2.5 f 1 2
114 2.5 f 1 2
115 3.3 f 1 2
116 2.0 f 1 2
117 2.0 f 1 2
118 2.0 f 1 2
119 2.0 f 1 2
120 2.7 f 1 2
121 2.7 f 1 2
122 2.7 f 1 2
123 3.0 4 1 1
124 3.7 4 1 1
125 4.0 4 1 1
126 4.7 4 1 1
127 4.7 4 1 1
128 4.7 4 1 1
129 5.7 4 1 1
130 6.1 4 1 1
131 4.0 4 1 1
132 4.2 4 1 1
133 4.4 4 1 1
134 4.6 4 1 1
135 5.4 r 1 3
136 5.4 r 1 3
137 5.4 r 1 3
138 4.0 4 1 1
139 4.0 4 1 1
140 4.6 4 1 1
141 5.0 4 1 1
142 2.4 f 1 2
143 2.4 f 1 2
144 2.5 f 1 2
145 2.5 f 1 2
146 3.5 f 1 2
147 3.5 f 1 2
148 3.0 f 1 2
149 3.0 f 1 2
150 3.5 f 1 2
151 3.3 4 1 1
152 3.3 4 1 1
153 4.0 4 1 1
154 5.6 4 1 1
155 3.1 f 1 2
156 3.8 f 1 2
157 3.8 f 1 2
158 3.8 f 1 2
159 5.3 f 1 2
160 2.5 4 1 1
161 2.5 4 1 1
162 2.5 4 1 1
163 2.5 4 1 1
164 2.5 4 1 1
165 2.5 4 1 1
166 2.2 4 1 1
167 2.2 4 1 1
168 2.5 4 1 1
169 2.5 4 1 1
170 2.5 4 1 1
171 2.5 4 1 1
172 2.5 4 1 1
173 2.5 4 1 1
174 2.7 4 1 1
175 2.7 4 1 1
176 3.4 4 1 1
177 3.4 4 1 1
178 4.0 4 1 1
179 4.7 4 1 1
180 2.2 f 1 2
181 2.2 f 1 2
182 2.4 f 1 2
183 2.4 f 1 2
184 3.0 f 1 2
185 3.0 f 1 2
186 3.5 f 1 2
187 2.2 f 1 2
188 2.2 f 1 2
189 2.4 f 1 2
190 2.4 f 1 2
191 3.0 f 1 2
192 3.0 f 1 2
193 3.3 f 1 2
194 1.8 f 1 2
195 1.8 f 1 2
196 1.8 f 1 2
197 1.8 f 1 2
198 1.8 f 1 2
199 4.7 4 1 1
200 5.7 4 1 1
201 2.7 4 1 1
202 2.7 4 1 1
203 2.7 4 1 1
204 3.4 4 1 1
205 3.4 4 1 1
206 4.0 4 1 1
207 4.0 4 1 1
208 2.0 f 1 2
209 2.0 f 1 2
210 2.0 f 1 2
211 2.0 f 1 2
212 2.8 f 1 2
213 1.9 f 1 2
214 2.0 f 1 2
215 2.0 f 1 2
216 2.0 f 1 2
217 2.0 f 1 2
218 2.5 f 1 2
219 2.5 f 1 2
220 2.8 f 1 2
221 2.8 f 1 2
222 1.9 f 1 2
223 1.9 f 1 2
224 2.0 f 1 2
225 2.0 f 1 2
226 2.5 f 1 2
227 2.5 f 1 2
228 1.8 f 1 2
229 1.8 f 1 2
230 2.0 f 1 2
231 2.0 f 1 2
232 2.8 f 1 2
233 2.8 f 1 2
234 3.6 f 1 2
[1] "XS: "
$`1`
[1] 1.8 1.8 2.0 2.0 2.8 2.8 3.1 3.1 2.8 3.1 4.2 5.3 5.3 5.7 6.5 3.7 3.7 3.9 3.9 4.7 4.7 4.7 5.2 5.2
[25] 3.9 4.7 4.7 4.7 5.2 5.7 5.9 4.7 4.7 4.7 4.7 4.7 4.7 5.2 5.2 5.7 5.9 4.0 4.0 4.0 4.0 4.6 5.0 4.2
[49] 4.2 4.6 4.6 4.6 5.4 5.4 3.0 3.7 4.0 4.7 4.7 4.7 5.7 6.1 4.0 4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