Changing Character Values into Categorical - r

I have two habitat types, forest and bog, but need to change them into categorical values so I can plot a log function with it. I just want to code forest as 0 and habitat as 1. How would I do this?
Here is what some of my data looks like:
Site Lat Long Elev nSpp Habitat
1 TPB 42.00333 73 389 6 forest
2 TPB 42.00333 73 389 5 bog
3 HBC 42.02889 70 8 16 forest
4 HBC 42.02889 70 8 6 bog
5 CKB 42.04944 71 152 18 forest
6 CKB 42.04944 71 152 14 bog

Base R
transform(data, Habitat = +(Habitat == "bog"))
# Site Lat Long Elev nSpp Habitat
#1 TPB 42.00333 73 389 6 0
#2 TPB 42.00333 73 389 5 1
#3 HBC 42.02889 70 8 16 0
#4 HBC 42.02889 70 8 6 1
#5 CKB 42.04944 71 152 18 0
#6 CKB 42.04944 71 152 14 1
The unary operator + coerces the logical expression Habitat == "bog" to an integer.
Sample data
data <- read.table(text = "Site Lat Long Elev nSpp Habitat
1 TPB 42.00333 73 389 6 forest
2 TPB 42.00333 73 389 5 bog
3 HBC 42.02889 70 8 16 forest
4 HBC 42.02889 70 8 6 bog
5 CKB 42.04944 71 152 18 forest
6 CKB 42.04944 71 152 14 bog", header = T)

You could use the following code which convert your character values to a defined value:
change <- c(forest = 0, bog = 1)
df$Habitat <- change[df$Habitat]
df
#> Site Lat Long Elev nSpp Habitat
#> 1 TPB 42.00333 73 389 6 0
#> 2 TPB 42.00333 73 389 5 1
#> 3 HBC 42.02889 70 8 16 0
#> 4 HBC 42.02889 70 8 6 1
#> 5 CKB 42.04944 71 152 18 0
#> 6 CKB 42.04944 71 152 14 1
Created on 2022-10-21 with reprex v2.0.2

Related

Join data frame into one in r

I have 4 data frames that all look like this:
Product 2018
Number
Minimum
Maximum
1
56
1
5
2
42
12
16
3
6523
23
56
4
123
23
102
5
56
23
64
6
245623
56
87
7
546
25
540
8
54566
253
560
Product 2019
Number
Minimum
Maximum
1
56
32
53
2
642
423
620
3
56423
432
560
4
3
431
802
5
2
2
6
6
4523
43
68
7
555
23
54
8
55646
3
6
Product 2020
Number
Minimum
Maximum
1
23
2
5
2
342
4
16
3
223
3
5
4
13
4
12
5
2
4
7
6
223
7
8
7
5
34
50
8
46
3
6
Product 2021
Number
Minimum
Maximum
1
234
3
5
2
3242
4
16
3
2423
43
56
4
123
43
102
5
24
4
6
6
2423
4
18
7
565
234
540
8
5646
23
56
I want to join all the tables so I get a table that looks like this:
Products
Number 2021
Min-Max 2021
Number 2020
Min-Max 2020
Number 2019
Min-Max 2019
Number 2018
Min-Max 2018
1
234
3 to 5
23
2 to 5
...
...
...
...
2
3242
4 to 16
342
4 to 16
...
...
...
...
3
2423
43 to 56
223
3 to 5
...
...
...
...
4
123
43 to 102
13
4 to 12
...
...
...
...
5
24
4 to 6
2
4 to 7
...
...
...
...
6
2423
4 to 18
223
7 to 8
...
...
...
...
7
565
234 to 540
5
34 to 50
...
...
...
...
8
5646
23 to 56
46
3 to 6
...
...
...
...
The Product for all years are the same so I would like to have a data frame that contains the number for each year as a column and joins the column for minimum and maximum as one.
Any help is welcome!
How about something like this. You are trying to join several dataframes by a single column, which is relatively straight forward using full_join. The difficulty is that you are trying to extract information from the column names and combine several columns at the same time. I would map out everying you want to do and then reduce the list of dataframes at the end. Here is an example with two dataframes, but you could add as many as you want to the list at the begining.
library(tidyverse)
#test data
set.seed(23)
df1 <- tibble("Product 2018" = seq(1:8),
Number = sample(1:100, 8),
Minimum = sample(1:100, 8),
Maximum = map_dbl(Minimum, ~sample(.x:1000, 1)))
set.seed(46)
df2 <- tibble("Product 2019" = seq(1:8),
Number = sample(1:100, 8),
Minimum = sample(1:100, 8),
Maximum = map_dbl(Minimum, ~sample(.x:1000, 1)))
list(df1, df2) |>
map(\(x){
year <- str_extract(colnames(x)[1], "\\d+?$")
mutate(x, !!quo_name(paste0("Min-Max ", year)) := paste(Minimum, "to", Maximum))|>
rename(!!quo_name(paste0("Number ", year)) := Number)|>
rename_with(~gsub("\\s\\d+?$", "", .), 1) |>
select(-c(Minimum, Maximum))
}) |>
reduce(full_join, by = "Product")
#> # A tibble: 8 x 5
#> Product `Number 2018` `Min-Max 2018` `Number 2019` `Min-Max 2019`
#> <int> <int> <chr> <int> <chr>
#> 1 1 29 21 to 481 50 93 to 416
#> 2 2 28 17 to 314 78 7 to 313
#> 3 3 72 40 to 787 1 91 to 205
#> 4 4 43 36 to 557 47 55 to 542
#> 5 5 45 70 to 926 52 76 to 830
#> 6 6 34 96 to 645 70 20 to 922
#> 7 7 48 31 to 197 84 6 to 716
#> 8 8 17 86 to 951 99 75 to 768
This is a similar answer, but includes bind_rows to combine the data.frames, then pivot_wider to end in a wide format.
The first steps strip the year from the Product XXXX column name, as this carries relevant information on year for that data.frame. If that column is renamed as Product they are easily combined (with a separate column containing the Year). If this step can be taken earlier in the data collection or processing timeline, it is helpful.
library(tidyverse)
list(df1, df2, df3, df4) %>%
map(~.x %>%
mutate(Year = gsub("Product", "", names(.x)[1])) %>%
rename(Product = !!names(.[1]))) %>%
bind_rows() %>%
mutate(Min_Max = paste(Minimum, Maximum, sep = " to ")) %>%
pivot_wider(id_cols = Product, names_from = Year, values_from = c(Number, Min_Max), names_vary = "slowest")
Output
Product Number_2018 Min_Max_2018 Number_2019 Min_Max_2019 Number_2020 Min_Max_2020 Number_2021 Min_Max_2021
<int> <int> <chr> <int> <chr> <int> <chr> <int> <chr>
1 1 56 1 to 5 56 32 to 53 23 2 to 5 234 3 to 5
2 2 42 12 to 16 642 423 to 620 342 4 to 16 3242 4 to 16
3 3 6523 23 to 56 56423 432 to 560 223 3 to 5 2423 43 to 56
4 4 123 23 to 102 3 431 to 802 13 4 to 12 123 43 to 102
5 5 56 23 to 64 2 2 to 6 2 4 to 7 24 4 to 6
6 6 245623 56 to 87 4523 43 to 68 223 7 to 8 2423 4 to 18
7 7 546 25 to 540 555 23 to 54 5 34 to 50 565 234 to 540
8 8 54566 253 to 560 55646 3 to 6 46 3 to 6 5646 23 to 56

find max column value in r conditional on another column

I have a data frame of baseball player information:
playerID nameFirst nameLast bats throws yearID stint teamID lgID G AB R H X2B X3B HR RBI SB CS BB SO IBB
81955 rolliji01 Jimmy Rollins B R 2007 1 PHI NL 162 716 139 212 38 20 30 94 41 6 49 85 5
103358 wilsowi02 Willie Wilson B R 1980 1 KCA AL 161 705 133 230 28 15 3 49 79 10 28 81 3
93082 suzukic01 Ichiro Suzuki L R 2004 1 SEA AL 161 704 101 262 24 5 8 60 36 11 49 63 19
83973 samueju01 Juan Samuel R R 1984 1 PHI NL 160 701 105 191 36 19 15 69 72 15 28 168 2
15201 cashda01 Dave Cash R R 1975 1 PHI NL 162 699 111 213 40 3 4 57 13 6 56 34 5
75531 pierrju01 Juan Pierre L L 2006 1 CHN NL 162 699 87 204 32 13 3 40 58 20 32 38 0
HBP SH SF GIDP average
81955 7 0 6 11 0.2960894
103358 6 5 1 4 0.3262411
93082 4 2 3 6 0.3721591
83973 7 0 1 6 0.2724679
15201 4 0 7 8 0.3047210
75531 8 10 1 6 0.2918455
I want to return a maximum value of the batting average ('average') column where the at-bats ('AB') are greater than 100. There are also 'NaN' in the average column.
If you want to return the entire row for which the two conditions are TRUE, you can do something like this.
library(tidyverse)
data <- tibble(
AB = sample(seq(50, 150, 10), 10),
avg = c(runif(9), NaN)
)
data %>%
filter(AB >= 100) %>%
filter(avg == max(avg, na.rm = TRUE))
Where the first filter is to only keep rows where AB is greater than or equal to 100 and the second filter is to select the entire row where it is max. If you want to to only get the maximum value, you can do something like this:
data %>%
filter(AB >= 100) %>%
summarise(max = max(avg, na.rm = TRUE))

Adding a column according to norm data in R

I have a longitudinal dataset in the long form with the length of around 2800, with around 400 participants in total. Here's a sample of my data.
# ID wave score sex age edu
#1 1001 1 28 1 69 12
#2 1001 2 27 1 70 12
#3 1001 3 28 1 71 12
#4 1001 4 26 1 72 12
#5 1002 1 30 2 78 9
#6 1002 3 30 2 80 9
#7 1003 1 30 2 65 16
#8 1003 2 30 2 66 16
#9 1003 3 29 2 67 16
#10 1003 4 28 2 68 16
#11 1004 1 22 2 85 4
#12 1005 1 20 2 60 9
#13 1005 2 18 1 61 9
#14 1006 1 22 1 74 9
#15 1006 2 23 1 75 9
#16 1006 3 25 1 76 9
#17 1006 4 19 1 77 9
I want to create a new column "cutoff" with values "Normal" or "Impaired" because my outcome data, "score" has a cutoff score indicating impairment according to norm. The norm consists of different -1SD measures(the cutoff point) according to Sex, Edu(year of education), and Age.
Below is what I'm currently doing, checking an excel file myself and putting in the corresponding cutoff score according to the three conditions. First of all, I am not sure if I am creating the right column.
data$cutoff <- ifelse(data$sex==1 & data$age<70
& data$edu<3
& data$score<19.91, "Impaired", "Normal")
data$cutoff <- ifelse(data$sex==2 & data$age<70
& data$edu<3
& data$score<18.39, "Impaired", "Normal")
Additionally, I am wondering if I can import an excel file stating the norm, and create a column according to the values in it.
The excel file has a structure as shown below.
# Sex Male Female
#60-69 Edu(yr) 0-3 4-6 7-12 13>= 0-3 4-6 7-12 13>=
#Age Number 22 51 119 72 130 138 106 51
# Mean 24.45 26.6 27.06 27.83 23.31 25.86 27.26 28.09
# SD 3.03 1.89 1.8 1.53 3.28 2.55 1.85 1.44
# -1.5SD' 19.92 23.27 23.76 24.8 18.53 21.81 23.91 25.15
#70-79 Edu(yr) 0-3 4-6 7-12 13>= 0-3 4-6 7-12 13>=
....
I have created new columns "agecat" and "educat," allocating each ID into a group of age and education used in the norm. Now I want to make use of these columns, matching it with rows and columns of the excel file above. One of the motivations is to create a code that can be used for further research using the test scores of my data.
I think your ifelse statements should work fine, but I would definitely import the Excel file rather than hardcoding it, though you may need to structure it a bit differently. I would structure it just like a dataset, with columns for Sex, Edu, Age, Mean, SD, -1.5SD, etc., import it into R, then do a left outer join on Sex+Edu+Age:
merge(x = long_df, y = norm_df, by = c("Sex", "Edu(yr)", "Age"), all.x = TRUE)
Then you can compare the columns directly.
If I understand correctly, the OP wants to mark a certain type of outliers in his dataset. So, there are two tasks here:
Compute the statistics mean(score), sd(score), and cutoff value mean(score) - 1.5 * sd(score) for each group of sex, age category agecat, and edu category edcat.
Find all rows where score is lower than the cutoff value for the particular group.
As already mentioned by hannes101, the second step can be implemented by a non-equi join.
library(data.table)
# categorize age and edu (left closed intervals)
mydata[, c("agecat", "educat") := .(cut(age, c(seq(0, 90, 10), Inf), right = FALSE),
cut(edu, c(0, 4, 7, 13, Inf), right = FALSE))][]
# compute statistics
cutoffs <- mydata[, .(.N, Mean = mean(score), SD = sd(score),
m1.5SD = mean(score) - 1.5 * sd(score)),
by = .(sex, agecat, educat)]
# non-equi update join
mydata[, cutoff := "Normal"]
mydata[cutoffs, on = .(sex, agecat, educat, score < m1.5SD), cutoff := "Impaired"][]
mydata
ID wave score sex age edu agecat educat cutoff
1: 1001 1 28 1 69 12 [60,70) [7,13) Normal
2: 1001 2 27 1 70 12 [70,80) [7,13) Normal
3: 1001 3 28 1 71 12 [70,80) [7,13) Normal
4: 1001 4 26 1 72 12 [70,80) [7,13) Normal
5: 1002 1 30 2 78 9 [70,80) [7,13) Normal
6: 1002 3 30 2 80 9 [80,90) [7,13) Normal
7: 1003 1 33 2 65 16 [60,70) [13,Inf) Normal
8: 1003 2 32 2 66 16 [60,70) [13,Inf) Normal
9: 1003 3 31 2 67 16 [60,70) [13,Inf) Normal
10: 1003 4 24 2 68 16 [60,70) [13,Inf) Impaired
11: 1004 1 22 2 85 4 [80,90) [4,7) Normal
12: 1005 1 20 2 60 9 [60,70) [7,13) Normal
13: 1005 2 18 1 61 9 [60,70) [7,13) Normal
14: 1006 1 22 1 74 9 [70,80) [7,13) Normal
15: 1006 2 23 1 75 9 [70,80) [7,13) Normal
16: 1006 3 25 1 76 9 [70,80) [7,13) Normal
17: 1006 4 19 1 77 9 [70,80) [7,13) Normal
18: 1007 1 33 2 65 16 [60,70) [13,Inf) Normal
19: 1007 2 32 2 66 16 [60,70) [13,Inf) Normal
20: 1007 3 31 2 67 16 [60,70) [13,Inf) Normal
21: 1007 4 31 2 68 16 [60,70) [13,Inf) Normal
ID wave score sex age edu agecat educat cutoff
In this made-up example there is only one row which meets the "Impaired" conditions.
Likewise, the statistics is rather sparsely populated:
cutoffs
sex agecat educat N Mean SD m1.5SD
1: 1 [60,70) [7,13) 2 23.00000 7.071068 12.39340
2: 1 [70,80) [7,13) 7 24.28571 3.147183 19.56494
3: 2 [70,80) [7,13) 1 30.00000 NA NA
4: 2 [80,90) [7,13) 1 30.00000 NA NA
5: 2 [60,70) [13,Inf) 8 30.87500 2.900123 26.52482
6: 2 [80,90) [4,7) 1 22.00000 NA NA
7: 2 [60,70) [7,13) 1 20.00000 NA NA
Data
OP's sample dataset has been modified in one group for demonstration.
library(data.table)
mydata <- fread("
# ID wave score sex age edu
#1 1001 1 28 1 69 12
#2 1001 2 27 1 70 12
#3 1001 3 28 1 71 12
#4 1001 4 26 1 72 12
#5 1002 1 30 2 78 9
#6 1002 3 30 2 80 9
#7 1003 1 33 2 65 16
#8 1003 2 32 2 66 16
#9 1003 3 31 2 67 16
#10 1003 4 24 2 68 16
#11 1004 1 22 2 85 4
#12 1005 1 20 2 60 9
#13 1005 2 18 1 61 9
#14 1006 1 22 1 74 9
#15 1006 2 23 1 75 9
#16 1006 3 25 1 76 9
#17 1006 4 19 1 77 9
#18 1007 1 33 2 65 16
#19 1007 2 32 2 66 16
#20 1007 3 31 2 67 16
#21 1007 4 31 2 68 16
", drop = 1L)

Stacked bar plot

I am plotting stacked bar graph of my large data set, it is work fine. But in my plot there is one extra bar is produced as named 'NA'. I don't know what is wrong so please help me. And i also want to change the color of bars and want to make it more visualizing. Is there any way to change the spectral color pattern like the law value's color are more light than dark.I want the color pattern as "white to dark blue" or any dark shade. My dataset,code and plot are as below:
Degree No.of.vertices Articulationpoint
1 2392 0
2 1439 140
3 981 104
4 698 88
5 579 73
6 445 77
7 366 74
8 272 55
9 255 39
10 226 49
11 179 46
12 146 32
13 121 30
14 124 34
15 95 25
16 88 28
17 96 39
18 70 17
19 81 34
20 64 20
21 59 22
22 45 20
23 41 19
24 38 17
25 28 14
26 30 18
27 29 16
28 28 13
29 22 9
30 18 11
31 16 6
32 15 9
33 17 11
34 14 5
35 25 11
36 14 6
37 8 7
38 19 10
39 9 3
40 14 6
41 9 2
42 9 4
43 10 6
44 7 5
45 8 4
46 3 2
47 5 4
48 10 8
49 8 4
50 3 1
51 5 5
52 5 5
53 8 6
54 4 2
55 3 3
56 3 2
57 6 5
58 2 2
59 6 4
60 2 2
61 5 4
62 5 2
63 3 3
64 5 4
65 1 0
66 3 2
67 3 2
68 1 1
69 2 0
70 6 6
71 2 0
72 4 4
73 5 5
74 7 6
75 1 1
76 1 1
77 2 2
79 1 1
81 1 0
82 1 1
83 2 2
84 4 2
85 2 2
86 1 0
87 1 1
88 2 2
89 2 2
90 2 2
91 1 1
92 1 1
96 3 3
97 1 1
100 1 1
101 2 1
102 2 2
103 1 1
104 1 0
106 1 1
108 2 1
109 1 1
110 1 1
112 1 1
113 2 2
115 2 1
116 1 1
117 2 2
119 1 1
122 1 0
124 1 1
127 2 1
128 1 1
130 1 1
134 2 2
144 1 1
145 1 1
147 1 1
150 1 1
151 1 1
152 2 2
154 1 1
160 1 0
161 1 1
165 1 1
168 1 1
172 1 1
180 1 1
188 1 1
193 1 1
198 1 1
207 1 1
209 1 1
246 1 1
269 1 1
My code is:
d <- read.csv("Data.csv");d
df <- data.frame(d);df
df$Degree <- cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134))
library(reshape2)
library(ggplot2)
ggplot(df, aes(x = Degree, y = No.of.vertices, fill = Articulationpoint)) +
geom_bar( stat = "identity", position = "stack", colour = "black" )
Which results in:
In my graph i want white background not that grey boxes. And i also want to fit the graph in 4*4 frame so please help me.
pls help me
Thanks in advance.
Inside your data preparation you are creating missing values, which are being displayed on the plot:
library(dplyr)
# while cutting you are creating missing values
cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134)) %>% tail(., 30)
#> [1] (100,134] (100,134] (100,134] (100,134] (100,134] (100,134] (100,134]
#> [8] (100,134] (100,134] (100,134] <NA> <NA> <NA> <NA>
#> [15] <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#> [22] <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#> [29] <NA> <NA>
#> 15 Levels: (0,1] (1,2] (2,3] (3,4] (4,5] (5,6] (6,7] (7,8] ... (100,134]
You could extend your intervals to include all the data, in case that is meaningful:
cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134, 270)) %>% tail(., 30)
#> [1] (100,134] (100,134] (100,134] (100,134] (100,134] (100,134] (100,134]
#> [8] (100,134] (100,134] (100,134] (134,270] (134,270] (134,270] (134,270]
#> [15] (134,270] (134,270] (134,270] (134,270] (134,270] (134,270] (134,270]
#> [22] (134,270] (134,270] (134,270] (134,270] (134,270] (134,270] (134,270]
#> [29] (134,270] (134,270]
#> 16 Levels: (0,1] (1,2] (2,3] (3,4] (4,5] (5,6] (6,7] (7,8] ... (134,270]
Alternatively, remove missing values before plotting:
df$Degree <- cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134))
df <- df[complete.cases(df), ]
Changing the background can be done with theme_bw, specifying different colors for the gradient by scale_fill_gradient.
library(ggplot2)
ggplot(df, aes(x = Degree, y = No.of.vertices, fill = Articulationpoint)) +
geom_bar(stat = "identity", position = "stack", colour = "black" ) +
scale_fill_gradient(high = "#ecf6fe", low = "#085695") + # change low and hig colours as you whish
theme_bw() # white background
Like lawyeR already mentioned, you can use ggsave to control the aspect ratio of your plot when you save it:
ggsave("myplot.png", name_of_plot, width = 4, height = 4)
Using your data and df <- read.table(textConnection("your data") I did some cleanup (might have used other arguments to read.table, instead).
df <- as.data.frame(df)
colnames(df) <- c("Degree", "No.of.vertices", "Articulationpoint")
df$Degree <- as.numeric(df$Degree)
df$No.of.vertices <- as.numeric(df$No.of.vertices)
df$Degree <- cut(df$Degree,c(0,1,2,3,4,5,6,7,8,9,10,25,50,75,100,134))
Then ran your plot code but added theme_bw():
p <- ggplot(df, aes(x = Degree, y = No.of.vertices, fill = Articulationpoint)) +
geom_bar( stat = "identity", position = "stack", colour = "black" ) +
theme_bw()
This removes the grey background but leaves a massive legend. I am not sure why your outcome differs from mine.
You can set your size dimensions as follows:
ggsave(p, filename = "your plot name.png", width = 4, height = 4, unit = "in")

how to fix "undefined columns selected" for network meta-analysis in R?

I am conducting a network meta-analysis on R with two packages, gemtc and rjags. However, when I type
Model <- mtc.model (network, linearmodel=’fixed’).
R always returns “
Error in [.data.frame(data, sel1 | sel2, columns, drop = FALSE) :
undefined columns selected In addition: Warning messages: 1: In
mtc.model(network, linearModel = "fixed") : Likelihood can not be
inferred. Defaulting to normal. 2: In mtc.model(network, linearModel =
"fixed") : Link can not be inferred. Defaulting to identity “
How to fix this problem? Thanks!
I am attaching my codes and data here:
SAE <- read.csv(file.choose(),head=T, sep=",")
head(SAE)
network <- mtc.network(data.ab=SAE)
summary(network)
plot(network)
model.fe <- mtc.model (network, linearModel="fixed")
plot(model.fe)
summary(model.fe)
cat(model.fe$code)
model.fe$data
# run this model
result.fe <- mtc.run(model.fe, n.adapt=0, n.iter=50)
plot(result.fe)
gelman.diag(result.fe)
result.fe <- mtc.run(model.fe, n.adapt=1000, n.iter=5000)
plot(result.fe)
gelman.diag(result.fe)
following is my data: SAE
study treatment responder sample.size
1 1 3 0 76
2 1 30 2 72
3 2 3 99 1389
4 2 23 132 1383
5 3 1 6 352
6 3 30 2 178
7 4 2 6 106
8 4 30 3 95
9 5 3 49 393
10 5 25 18 198
11 6 1 20 65
12 6 22 10 26
13 7 1 1 76
14 7 30 3 76
15 8 3 7 441
16 8 26 1 220
17 9 2 1 47
18 9 30 0 41
19 10 3 10 156
20 10 30 9 150
21 11 1 4 85
22 11 25 5 85
23 11 30 4 84
24 12 3 6 152
25 12 30 5 160
26 13 18 4 158
27 13 21 8 158
28 14 1 3 110
29 14 30 2 111
30 15 3 3 83
31 15 30 1 92
32 16 1 3 124
33 16 22 6 123
34 16 30 4 125
35 17 3 236 1553
36 17 23 254 1546
37 18 6 5 398
38 18 7 6 403
39 19 1 64 588
40 19 22 73 584
How about reading the manual ?mtc.model. It clearly states the following:
Required columns [responders, sampleSize]
So your responder variable should be responders and your sample.size variable should be sampleSize.
Next, your plot(network) should help you determine that some comparisons can not be made. In your data, there are 2 subgroups of trials that were compared. Treatment 18 and 21 were not compared with any of the others. Therefore you can only do a meta-analysis of 21 and 18 or a network meta-analysis of the rest.
network <- mtc.network(data.ab=SAE[!SAE$treatment %in% c(21, 18), ])
model.fe <- mtc.model(network, linearModel="fixed")

Resources