Merge horizontally only for certain columns flextable - r

I have the following table:
tmp <- structure(list(SOC = c("Blood", "", "", "Gast", "", "", "", "Skin",
"", "", "Adverse Event"), `Adverse Event` = c("Blood", "Raised Alt", "Raised Ast",
"Gast", "Bloating", "Diarrhoia", "Vomiting", "Skin", "Reddness",
"Rash", "Any Adverse Event"), C11 = c("", "0", "0", "", "0",
"2", "0", "", "0", "0", "2"), C21 = c("", "0", "0", "", "1",
"0", "1", "", "1", "0", "3"), T1 = c("", "0", "0", "", "1", "2",
"1", "", "1", "0", "3"), C12 = c("", "1", "0", "", "0", "0",
"0", "", "0", "1", "2"), C22 = c("", "0", "0", "", "0", "0",
"1", "", "0", "0", "1"), T2 = c("", "1", "0", "", "0", "0", "1",
"", "0", "1", "2"), C23 = c("", "0", "1", "", "0", "0", "0",
"", "0", "0", "1"), T3 = c("", "0", "1", "", "0", "0", "0", "",
"0", "0", "1"), C14 = c("", "1", "0", "", "0", "0", "0", "",
"0", "0", "1"), T4 = c("", "1", "0", "", "0", "0", "0", "", "0",
"0", "1")), row.names = c(NA, 11L), class = "data.frame")
I have turned it into a flextable like this:
tmp %>% regulartable()
And now I am trying to horizontally merge the matching values ONLY in the SOC and Adverse Event columns.
I have tried using merge_h() but that doesn't give me the option to select certain columns, so it merges all of the other columns as well if there are duplicated values.
I have tried merge_at() but it doesn't work if all of the i and j values are not consecutive, which mine wont be.
Does anyone know of a way to only make merge_h() apply to certain columns? Or any other way of achieving what I'm after?
EDIT: I'm trying to make a flextable that looks a bit like this, but without any of the numeric columns being merged. As you can see in the bottom right hand corner all of the 1's have been merged. I just want the first two columns to merge so I can create the indentation effect.

You can create a for-loop iterating over the lines in question and then only merge the first two columns:
lines <- c(1, 5, 7, 10)
for (ll in lines){
tmp <- merge_at(i = ll, j = 1:2, part = "body")
}
Might not be the most elegant, but it will do what you need

Related

How to convert a range of columns from Character to Number/Integer in R

I am tryin to convert a few columns which are in a range from Character to Integer. I dont want to write each column as.integer.
I am trying to find a more effective way where I can pass the the column names which I want to convert and then convert them into integer.
Is this doable in R? Or Should I do it one column after the other.
The Expected output:
Convert a range of data which is in char to Integer.
Convert a few columns without using passing them as range but rather as individual columns.
The code I wrote is given below:
library(readxl)
Final <- read_excel("C:/X/X/X- X/Desktop/Final.xlsx")
First_Date <- colnames(Final)[4]
Last_Date <- tail(colnames(Final),1)
str(Final)
Final <- Final %>%
mutate_if(c(First_Date:Last_Date),as.numeric)
The data I am working with is given below:
structure(list(UniqueID = c("3F-FA|807905", "3F-FA|808005", "3F-FA|808006",
"3F-FA|808007", "Py_AuAriFa|761403", "3F-FA|761502", "AutoTheta|761602",
"3F-FA|318901", "3F-FA|339401"), Xreg = c("3F-FA", "3F-FA", "3F-FA",
"3F-FA", "Py_AuAriFa", "3F-FA", "AutoTheta", "3F-FA", "3F-FA"
), Row = c("807905", "808005", "808006", "808007", "761403",
"761502", "761602", "318901", "339401"), `2023-02-01` = c("0",
"0", "0", "0", "50", "1", "7", "0", "0"), `2023-03-01` = c("0",
"0", "0", "0", "32", "1", "7", "0", "0"), `2023-04-01` = c("0",
"0", "0", "0", "36", "1", "7", "0", "0"), `2023-05-01` = c("0",
"0", "0", "0", "41", "1", "7", "0", "0"), `2023-06-01` = c("0",
"0", "0", "0", "31", "1", "6", "0", "0"), `2023-07-01` = c("0",
"0", "0", "0", "38", "1", "6", "0", "0"), `2023-08-01` = c("0",
"0", "0", "0", "34", "1", "6", "0", "0"), `2023-09-01` = c("0",
"0", "0", "0", "32", "1", "6", "0", "0"), `2023-10-01` = c("0",
"0", "0", "0", "35", "1", "5", "0", "0")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -9L))
The columns I am trying to convert is from 2023-02-01 to 2023-10-01. I cant use mutateif and pass it through the whole dataframe as the column Row has data which are character and can be converted to integer but should not be converted. Hence the selected few columns.
We can match the patterns in the column names to loop over those column and modify the class
library(dplyr)
Final <- Final %>%
mutate(across(matches("^\\d{4}-\\d{2}-\\d{2}$"), as.integer))
Or use the :
Final <- Final %>%
mutate(across("2023-02-01":"2023-10-01", as.integer))

How to wrangle the dataset in R: reshaping and creating new columns with given information

I have a dataset that looks like below,
structure(list(nonyeasted_19 = c("Force (N)", "0", "-0.0077",
"0.0023", "-0.0707", "-0.2155", "-0.2026", "-0.0628", "-0.0481",
"-0.0601", "0.0302", "0.0475", "-0.0176", "0.008", "0.0569",
"0.0242", "0.0003", "0.0295", "0.028", "-0.0221", "-0.0333",
"0.0034", "0.004", "-0.0219", "-0.0216", "-0.0261"), nonyeasted_19.1 = c("Distance (m)",
"0", "0", "0", "0", "0", "0", "0.000002", "0.000004", "0.000006",
"0.000008", "0.00001", "0.000012", "0.000014", "0.000016", "0.000018",
"0.00002", "0.000022", "0.000024", "0.000026", "0.000028", "0.00003",
"0.000032", "0.000034", "0.000036", "0.000038"), nonyeasted_19.2 = c("Time (sec)",
"0", "0.002", "0.004", "0.006", "0.008", "0.01", "0.012", "0.014",
"0.016", "0.018", "0.02", "0.022", "0.024", "0.026", "0.028",
"0.03", "0.032", "0.034", "0.036", "0.038", "0.04", "0.042",
"0.044", "0.046", "0.048"), nonyeasted_19.3 = c("Status", "101",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"), yeasted_01 = c("Force (N)",
"0", "0.0024", "0.0307", "-0.0487", "-0.2063", "-0.1928", "-0.0421",
"-0.0278", "-0.0586", "0.0251", "0.0373", "-0.0084", "0.0597",
"0.091", "0.0246", "0.0318", "", "", "", "", "", "", "", "",
""), yeasted_01.1 = c("Distance (m)", "0", "0", "0", "0", "0",
"0", "0", "0.000001", "0.000003", "0.000005", "0.000007", "0.000009",
"0.000011", "0.000013", "0.000015", "0.000017", "", "", "", "",
"", "", "", "", ""), yeasted_01.2 = c("Time (sec)", "0", "0.002",
"0.004", "0.006", "0.008", "0.01", "0.012", "0.014", "0.016",
"0.018", "0.02", "0.022", "0.024", "0.026", "0.028", "0.03",
"", "", "", "", "", "", "", "", ""), yeasted_01.3 = c("Status",
"101", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "", "", "", "", "", "", "", "", "")), class = "data.frame", row.names = c(NA,
-26L))
Every four columns are in one group, and the group names are in the first row, while the column names are in the second row. I wonder whether there are any ways to concatenate the groups vertically and create two new columns with the group name row, where column 1 contains the text before the underscore and column 2 contains the text after the underscore.
I tried to use tidyverse, but after read.csv(), the variable names could not be preserved.
one approach:
sample data (example_data.csv):
group_A,group_A,group_B,group_B
var_1,var_2,var_3,var_4
143,897,234,382
Code:
library(readr) ## for the read_lines function
library(tidyr) ## wrangling (pivoting etc.)
## read csv but skip first line (containing group names):
df <- read.csv('path/to/example_data.csv',skip = 1)
## read first line of csv and convert it to vector of group names:
group_names <- read_lines('path/to/example_data.csv', n_max = 1) %>%
strsplit(',') %>% unlist
## change names of dataframe df to: variable_name;group_name
names(df) <- paste(group_names, names(df), sep = ';')
## wrangle data (for documentation see https://tidyr.tidyverse.org/ )
df %>%
pivot_longer(everything(), names_to = 'group_var', values_to = 'value') %>%
separate(group_var, into = c('group', 'var'), sep = ';') %>%
separate(group, into = c('yeasted_status', 'index'), sep='_') %>%
pivot_wider(names_from = var, values_from = value)
Result:
## A tibble: 2 x 6
# yeasted_status index var_1 var_2 var3 var4
# <chr> <chr> <int> <int> <int> <int>
# 1 group A 143 897 NA NA
# 2 group B NA NA 234 382
edit
or, if df is the dataframe derived from your dput output:
df[-1,] %>%
pivot_longer(everything(),names_to = 'group_var', values_to = 'value') %>% head %>%
mutate(ID = paste(row_number(),group_var)) %>%
separate(group_var, into = c('group', 'var'), sep = ';') %>%
separate(group, into = c('yeasted_status', 'index'), sep='_') %>%
mutate(value = as.double(value)) %>%
pivot_wider(id_cols = c(ID, yeasted_status,index), names_from = var, values_from = value) %>%
select(-ID)

Plotting multiple binary variables on the same plot in ggplot

I am hoping to use ggplot to construct a barplot of frequencies (or just % 1s) of a bunch of binary variables, and am having trouble getting them all together on one plot.
The variables all stem from the same question in a survey, so ideally it'd be nice to have data that is tidy with one column for this variable, but respondents could select more than one option and I'm hoping to retain that instead of having a "more than one selected" option. Here is a slice of the data:
structure(list(gender = structure(c("Male", "Male", "Female",
"Female", "Female", "Female", "Male", "Male", "Male", "Male"), label = "Q4", format.stata = "%24s"),
var1 = structure(c("0", "0", "1", "1", "0", "0", "0", "0",
"0", "0"), format.stata = "%9s"), var2 = structure(c("0",
"98", "1", "0", "0", "0", "0", "0", "0", "0"), format.stata = "%9s"),
var3 = structure(c("0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"), format.stata = "%9s"), var4 = structure(c("1",
"0", "1", "0", "0", "0", "1", "1", "0", "0"), format.stata = "%9s"),
var5 = structure(c("1", "0", "0", "0", "0", "1", "0", "0",
"0", "0"), format.stata = "%9s")), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))
Get the data in long format so that it is easier to plot.
library(tidyverse)
df %>%
pivot_longer(cols = starts_with('var')) %>%
group_by(name) %>%
summarise(frequency_of_1 = sum(value == 1)) %>%
#If you need percentage use mean instead of sum
#summarise(frequency_of_1 = mean(value == 1)) %>%
ggplot() + aes(name, frequency_of_1) + geom_col()
In base R you can do this with colSums and barplot.
barplot(colSums(df[-1] == 1))
#For percentage
#barplot(colMeans(df[-1] == 1))

set correct column names when output list items to csv in r

Given a list (e.g. out), I want to write each item out to a separate csv file for use elsewhere.
The list is large and contains a lot of items so I wanted to shortcut using a for loop. I created the following to
build a name for each output file based on the data group and date. When I run it everything works except it renames the columns
using the list item name and the existing colnames (e.g. instead of 'week4' I get 'pygweek4'. I do not want it to change my column names.
I tried setting col.names = TRUE, hoping to retain the existing names, and using the code below to specify the names,
as well as setting col.names = FALSE. In all cases I get a warning message saying that "attempt to set 'col.names' ignored".
Can anyone suggest a simple method of retaining the column names I already have?
out <- list(pyg = structure(list(week4 = c("0", "1", "1", "0", "1"),
week5 = c("0", "1", "1", "1", "1"), week6 = c("0", "1", "0", "1", "1"),
week7 = c("0", "0", "0", "1", "1"), week8 = c("0", "1", "0", "1", "1")),
row.names = 281:285, class = "data.frame"),
saw = structure(list(week4 = c("0", "0", "0", "0", "0"),
week5 = c("0", "0", "0", "0", "0"), week6 = c("0", "0", "0", "0", "0"),
week7 = c("0", "0", "0", "0", "0"), week8 = c("0", "0", "0", "0", "1")),
row.names = c(NA, 5L), class = "data.frame"))
for(i in 1:length(out)){
n = paste(paste(names(out)[i],Sys.Date(), sep = "_"), ".csv", sep = "") # create set name and version control
write.csv(out[i], file = n, row.names = FALSE, col.names = c("week4", "week5", "week6", "week7", "week8"))
}
Sorry for the lack of decent tags... I don't have the reputation to set tags that I think are useful for this post and couldn't find ones that made sense in the ones available.
We don't need to specify the col.names. The issue seems to be that, the list elements are not extracted correctly. It should be [[i]] instead of [i]. With [i], it is still a list of one data.frame element. By doing [[i]], it extracts the data.frame from the list
for(i in seq_along(out)){
n <- paste(paste(names(out)[i],Sys.Date(), sep = "_"),
".csv", sep = "")
write.csv(out[[i]], file = n, row.names = FALSE, quote = FALSE)
}
The difference can be found from checking the str
str(out[[1]])
str(out[1])

Reshape from wide to long, but receiving "Erro: Each variable must have a unique name."

I'm trying to reshape this sort of dat from wide to long, but I'm consistently receiving an error message. What am I doing wrong?
The following is the command I'm using and next is a snippet of my data.
dat <- gather(data, key = "Discriminação", value = "Valor", 2:25)
data = structure(list(Discriminação = c("I. RECEITA TOTAL", "I.1 - Receita Administrada pela RFB",
"I.1.1 Imposto de Importação", "I.1.2 IPI", "I.1.2.1 IPI - Fumo",
"I.1.2.2 IPI - Bebidas", "I.1.2.3 IPI - Automóveis", "I.1.2.4 IPI - Vinculado a importação",
"I.1.2.5 IPI - Outros", "I.1.3 Imposto de Renda", "I.1.3.1 I.R. - Pessoa Física",
"I.1.3.2 I.R. - Pessoa Jurídica", "I.1.3.3 I.R. - Retido na Fonte",
"I.1.3.3.1 IRRF - Rendimentos do Trabalho", "I.1.3.3.2 IRRF - Rendimentos do Capital",
"I.1.3.3.3 IRRF - Remessas ao Exterior", "I.1.3.3.4 IRRF - Outros Rendimentos",
"I.1.4 IOF", "I.1.5 COFINS", "I.1.6 PIS/PASEP", "I.1.7 CSLL",
"I.1.8 CPMF", "I.1.9 CIDE Combustíveis", "I.1.10 Outras",
"I.2 - Incentivos Fiscais", "I.3 - Arrecadação Líquida para o RGPS",
"I.3.1 Urbana", "I.3.2 Rural", "I.4 - Receitas Não Administradas pela RFB",
"I.4.1 Concessões e Permissões", "I.4.2 Dividendos e Participações",
"I.4.3 Contr. Plano de Seguridade Social do Servidor", "I.4.4 CotaParte de Compensações Financeiras",
"I.4.5 Receitas Próprias (fontes 50, 81 e 82)", "I.4.6 Contribuição do Salário Educação",
"I.4.7 Complemento para o FGTS (LC nº 110/01)", "I.4.8 Operações com Ativos",
"I.4.9 Demais Receitas", "II. TRANSF. POR REPARTIÇÃO DE RECEITA ²",
"II.1 FPM / FPE / IPI-EE", "II.2 Fundos Constitucionais", "II.2.1 Repasse Total",
"II.2.2 Superávit dos Fundos", "II.3 Contribuição do Salário Educação",
"II.4 Compensações Financeiras", "II.5 CIDE - Combustíveis",
"II.6 Demais ", "III. RECEITA LÍQUIDA (I-II)"), `35431` = c("12290.48402097",
"7771.686318", "308.33310599999999", "1154.2698489999998", "239.76640076000001",
"0", "0", "0", "914.50344823999978", "3078.6463939999999", "94.157627899999994",
"746.44417759999999", "2193.4616628099998", "1021.97845", "933.9063329999999",
"141.84613899999999", "95.73074081", "286.14657599999998", "1609.839849",
"633.52791100000013", "566.52460299999996", "0", "0", "134.39803000000001",
"-43.786000000000001", "3558.9", "n.d.", "n.d.", "1003.6837029700005",
"0", "1.018E-3", "143.91146118", "0", "0", "219.19327398999999",
"0", "0", "640.57794980000051", "2501.2770794613334", "2219.0989999999997",
"142.74600000000001", "n.d.", "n.d.", "116.90307946133339", "0",
"0", "22.529000000000423", "9789.2069415086662"), `35462` = c("11596.253605349999",
"7412.7677630000007", "373.71706499999999", "1370.349948", "265.70880774",
"0", "0", "0", "1104.6411402599999", "2289.4818289999998", "96.123338219999994",
"878.31575730999998", "1251.9269735099997", "832.32662199999993",
"217.02893699999998", "98.032178000000002", "104.53923650999977",
"224.96925100000001", "1484.127166", "593.81305099999997", "487.84078900000003",
"517.02352699999994", "0", "71.445137000000003", "-54.344999999999999",
"3328.8999999999996", "n.d.", "n.d.", "908.93084234999878", "0",
"79.841385000000002", "106.13582145999999", "0", "0", "223.46032242999999",
"0", "0", "499.49331345999883", "1931.8750075993335", "1699.558",
"106.44", "n.d.", "n.d.", "119.17883862933338", "0", "0", "6.6981689700000828",
"9664.378597750665"), `35490` = c("13069.057196370002", "8772.0905360000015",
"392.20991500000002", "1333.5901859999999", "224.42094527999998",
"0", "0", "0", "1109.1692407199998", "3109.7138089999999", "88.046134890000005",
"1602.69746854", "1359.1085645200001", "912.31195400000001",
"256.197971", "84.837182999999996", "105.76145652000002", "248.65517800000001",
"1433.108508", "548.6668360000001", "1075.9043630000001", "566.18171600000005",
"0", "64.06002500000001", "0", "3321.2", "n.d.", "n.d.", "975.76666037000086",
"0", "43.971899000000001", "105.09224094999998", "0", "0", "220.03384943999998",
"0", "0", "606.66867098000091", "1770.4347517780002", "1550.672",
"97.075999999999993", "n.d.", "n.d.", "117.35138636800005", "0",
"0", "5.3353654100000796", "11298.622444592002"), `35521` = c("13388.171682420005",
"9102.386582000001", "425.32219399999997", "1359.284204", "238.45872682999999",
"0", "0", "0", "1120.8254771700001", "3362.6831529999995", "639.58441151",
"1077.2064527799998", "1572.6546193000001", "1027.6666290000001",
"329.77585899999997", "103.102914", "112.10921730000024", "292.27561700000001",
"1535.004101", "590.17022700000007", "811.20736099999999", "668.49842200000001",
"0", "57.941302999999998", "-29", "3398.1", "n.d.", "n.d.", "916.68510042000389",
"0", "6.4229659999999997", "134.95704977", "0", "0", "215.71615711000001",
"0", "0", "559.58892754000385", "2164.2085945553331", "1925.537",
"121.738", "n.d.", "n.d.", "115.04861712533341", "0", "0", "1.8849774299998217",
"11223.963087864669"), `35551` = c("12508.213110380002", "8007.4372199999998",
"382.67831999999999", "1361.4987289999999", "227.75285596999998",
"0", "0", "0", "1133.74587303", "2629.7056899999998", "330.61697849000001",
"788.03873327000008", "1449.82330075", "929.57523800000001",
"280.78663299999999", "117.78514399999999", "121.67628575000002",
"312.07559699999996", "1581.3016440000001", "608.18087200000002",
"479.33566099999996", "594.38239399999998", "0", "58.278313000000004",
"-45", "3473.2309999999998", "n.d.", "n.d.", "1072.5448903800025",
"0", "232.28614400000001", "126.14749049", "0", "0", "218.42420874999999",
"0", "0", "495.68704714000251", "2203.5913893333332", "1961.6490000000003",
"124.236", "n.d.", "n.d.", "116.4929113333334", "0", "0", "1.2134779999995544",
"10304.621721046669"), `35582` = c("11824.056829910003", "7705.8808960000006",
"423.258465", "1404.9834140000003", "235.48867934", "0", "0",
"0", "1169.4947346600002", "2172.5273669999997", "283.86115847000002",
"869.88687890000006", "950.12790891999998", "421.46072599999997",
"308.02626100000003", "112.414507", "108.22641492000001", "341.44264300000003",
"1615.697032", "609.43273999999997", "498.90150299999999", "575.47173999999995",
"0", "64.165992000000003", "-155.38499999999999", "3473.1", "n.d.",
"n.d.", "800.46093391000204", "0", "4.4937019999999999", "92.34258797999999",
"0", "0", "219.36043831000001", "0", "0", "484.26420562000203",
"1687.9608151974346", "1553.1890000000001", "17.047952182101312",
"n.d.", "n.d.", "116.9922337653334", "0", "0", "0.73162924999984114",
"10136.096014712566"), `35612` = c("13248.000809469999", "8486.7798770000009",
"451.13639899999998", "1307.0941879999998", "205.0856119", "0",
"0", "0", "1102.0085760999998", "2775.5193640000002", "281.91281991",
"1036.98037765", "1386.13954515", "680.54449199999988", "453.92956500000003",
"122.309729", "129.35575915000018", "401.24825700000002", "1561.7878549999998",
"602.17614700000001", "572.1337769999999", "756.63713199999995",
"0", "59.046757999999997", "-55", "3592.21", "n.d.", "n.d.",
"1224.0109324699997", "135.4", "134.364631", "124.67114504",
"0", "0", "206.50295422000002", "0", "0", "623.07220220999966",
"1830.6459670304512", "1652.61", "64.672273503117964", "n.d.",
"n.d.", "110.13490891733339", "0", "0", "3.22878460999992", "11417.354842439549"
), `35643` = c("13393.354350400001", "7696.924801000001", "445.56037600000002",
"1414.2827909999999", "239.13283902000001", "0", "0", "0", "1175.1499519799997",
"2159.3157150000002", "258.32754231000001", "850.98628030999998",
"962.61133146999998", "434.42924499999998", "316.82622800000001",
"88.554198", "122.80166047", "346.66533199999998", "1592.6949499999998",
"627.44175500000006", "471.95965699999999", "581.04766600000005",
"0", "57.956559000000006", "-145.59299999999999", "3575.68",
"n.d.", "n.d.", "2266.3425494000007", "1381.2222220000001", "1.8461559999999999",
"100.41527470000001", "0", "0", "227.72102355999999", "0", "0",
"555.13787314000047", "1896.733564378198", "1735.5420000000001",
"36.422213802864533", "n.d.", "n.d.", "121.45121256533339", "0",
"0", "3.3181380099999558", "11496.620786021802"), `35674` = c("12771.906180679995",
"8172.117729999999", "456.30073599999997", "1496.796775", "254.93903693999999",
"0", "0", "0", "1241.85773806", "2394.323832", "260.72712253999998",
"954.72709888999998", "1078.19987674", "534.580421", "312.35145399999999",
"118.588047", "112.67995473999999", "349.10860300000002", "1670.110428",
"650.45497", "522.62379299999998", "577.63895000000002", "0",
"54.759643000000004", "0", "3646.326", "n.d.", "n.d.", "953.46245067999553",
"0", "47.531815000000002", "75.947586709999996", "0", "0", "226.17218356000001",
"0", "0", "603.81086540999559", "1958.5167068121268", "1790.6486545400003",
"44.919080286793402", "n.d.", "n.d.", "120.62516456533339", "0",
"0", "2.3238074199998948", "10813.389473867868"), `35704` = c("13497.085229590002",
"8948.6852589999999", "488.582157", "1398.1782039999998", "206.50459298999999",
"0", "0", "0", "1191.6736110099998", "2957.9442980000003", "113.77742537",
"1079.4770321900003", "1706.64245218", "850.165255", "416.97395700000004",
"296.84378900000002", "142.65945117999999", "405.07523200000003",
"1651.783981", "645.21165999999994", "606.19046400000002", "718.54235500000004",
"0", "77.176907999999997", "-60", "3667.1550000000002", "n.d.",
"n.d.", "941.24497059000214", "0", "65.990613999999994", "94.702495609999986",
"0", "0", "223.29663748000002", "0", "0", "557.2552235000021",
"1994.2202348010032", "1829.7845294900003", "42.376053231670284",
"n.d.", "n.d.", "119.0915399893334", "0", "0", "2.9681120899993942",
"11502.864994788999"), `35735` = c("12991.686654489999", "8542.9044609999983",
"491.13259999999997", "1506.5236199999999", "249.40052285000002",
"0", "0", "0", "1257.1230971499999", "2572.6663779999999", "101.30243606000001",
"775.02217561999998", "1639.9796805200001", "838.34985800000004",
"523.13488100000006", "163.304834", "115.19010752", "275.934732",
"1674.904131", "698.08664899999997", "606.70254", "654.10314800000003",
"0", "62.850663000000004", "-30", "3639.81", "n.d.", "n.d.",
"838.97219349000034", "0.1", "35.594844999999999", "66.410481389999987",
"0", "0", "221.62905022999999", "0", "0", "515.23781687000042",
"2208.9495432265444", "2022.8302456900001", "66.091460943877934",
"n.d.", "n.d.", "118.20216012266673", "0", "0", "1.8256764699997916",
"10782.737111263456"), `35765` = c("18434.105477189998", "11091.329897",
"497.02492599999999", "1445.887154", "252.60338658000001", "0",
"0", "0", "1193.28376742", "4517.476138", "118.61851373", "1553.6552950299999",
"2692.67334127", "1621.9819199999999", "660.90716099999997",
"230.42196300000001", "179.36229726999997", "300.42980699999998",
"1702.6105639999998", "775.21188100000006", "989.49197099999992",
"699.83304799999996", "0", "163.364408", "-130", "5586.1449999999995",
"n.d.", "n.d.", "1886.6305801899971", "0.47", "170.00148999999999",
"129.11397820000002", "0", "0", "353.95662580000004", "0", "0",
"1233.088486189997", "2095.2077072981447", "1859.46607189", "44.97314204481151",
"n.d.", "n.d.", "188.77686709333346", "0", "0", "1.9916262700000402",
"16338.89776989185"), `35796` = c("15390.441466870003", "10906.728187999999",
"510.74404200000004", "1194.96523", "241.5020207", "0", "0",
"0", "953.46320930000002", "5012.6352479999996", "115.06900131",
"1117.4747719700001", "3712.0042102699999", "1234.072263", "2185.9418620000001",
"173.76347000000001", "118.22661527", "339.24859900000001", "1615.2497609999998",
"652.59278400000005", "714.61072799999988", "740.71939499999996",
"0", "125.962401", "-127.514", "3701.1030000000005", "n.d.",
"n.d.", "910.12427887000149", "0.48799999999999955", "0.40454600000000002",
"126.33518565", "0", "0", "208.55596287", "0", "0", "574.34058435000156",
"2712.2949299640004", "2523.3347943799999", "39.034000000000006",
"n.d.", "n.d.", "111.22984686400005", "0", "0", "38.69628872000024",
"12678.146536906002"), `35827` = c("13199.191340049998", "9024.2133329999997",
"412.09669500000001", "1429.5953810000001", "271.21373493999999",
"0", "0", "0", "1158.3816460600001", "3459.7957259999994", "87.531837190000005",
"1022.9946357300001", "2286.7400979599997", "1111.0554549999999",
"930.38229699999999", "130.980435", "114.32191096", "344.51977299999999",
"1437.4495529999999", "567.04374699999994", "702.27485899999999",
"595.18275700000004", "0", "76.254841999999996", "-80.305999999999997",
"3562.4070000000002", "n.d.", "n.d.", "692.87700704999952", "8.9999999999999993E-3",
"2.6236839999999999", "98.956290799999991", "0", "0", "226.58065503999998",
"0", "0", "364.70737720999955", "1832.2064870213333", "1672.6220356900001",
"37.439", "n.d.", "n.d.", "120.84301602133338", "0", "0", "1.3024353099997796",
"11366.984853028665"), `35855` = c("17814.424200450001", "10507.180350000001",
"556.83885999999995", "1289.0359209999999", "191.71630832", "0",
"0", "0", "1097.3196126799999", "4680.9593180000002", "102.51073916",
"2297.39701212", "2171.3930839099999", "1193.0520549999999",
"698.44958599999995", "165.79492000000002", "114.09652291", "325.52037000000001",
"1403.5725430000002", "592.41761300000007", "998.07398899999998",
"579.60433399999999", "0", "81.157402000000005", "-42.335999999999999",
"3591.8740000000003", "n.d.", "n.d.", "3757.7058504500005", "2453.0899109999991",
"8.6000000000000003E-5", "147.64468979", "0", "0", "177.76331181",
"0", "0", "979.20785185000159", "2710.8970393320005", "2554.4266379299997",
"51.35199999999999", "n.d.", "n.d.", "94.807099632000046", "0",
"0", "10.311301770000512", "15103.527161118"), `35886` = c("15669.699987049993",
"9697.2334009999977", "543.17575299999999", "1384.5818419999998",
"206.25575233000001", "0", "0", "0", "1178.3260896699999", "3798.6548489999996",
"731.94820131000006", "1010.5392300899998", "1976.5378851699998",
"1131.250996", "583.61334599999998", "127.85935900000001", "133.81418416999975",
"380.929104", "1547.962796", "634.12534000000005", "581.09353999999996",
"733.48923600000001", "0", "93.220940999999996", "-189.21700000000001",
"3507.9589999999998", "n.d.", "n.d.", "2653.724586049997", "1588.588086",
"71.025681000000006", "96.704806160000018", "0", "0", "174.58019000000002",
"0", "0", "722.82582288999674", "2548.3244588966663", "2418.5344674899998",
"27.272000000000006", "n.d.", "n.d.", "93.109434666666729", "0",
"0", "9.4085567399998808", "13121.37552815333"), `35916` = c("13517.95185123",
"8287.8721819999992", "527.16130199999998", "1382.4197120000001",
"194.20041621999999", "0", "0", "0", "1188.21929578", "2777.997081",
"336.04507410999997", "600.85284999999999", "1769.0233771999999",
"1055.4177319999999", "450.46854500000001", "150.51565199999999",
"112.6214482", "344.744125", "1586.856053", "588.91645799999992",
"383.520757", "624.449523", "0", "71.807171000000011", "-15.87",
"3781.4029999999998", "n.d.", "n.d.", "1464.546669230001", "4.4999999999999998E-2",
"620.02518599999996", "85.884310259999978", "0", "0", "188.65096781999998",
"0", "0", "569.94120515000111", "2404.2200649639999", "2245.1003144799993",
"57.012999999999977", "n.d.", "n.d.", "100.61384950400003", "0",
"0", "1.4929009800005986", "11113.731786266002"), `35947` = c("12474.79400794",
"7781.8275079999994", "547.44252399999993", "1405.8066799999999",
"202.18905785999999", "0", "0", "0", "1203.6176221399999", "2228.9695919999999",
"304.28720045", "711.52925444000005", "1146.5436723399998", "418.26559699999984",
"474.34253999999999", "138.20045400000001", "115.73508133999999",
"319.93960700000002", "1568.9624999999999", "597.44099800000004",
"428.17352", "608.87003700000002", "0", "76.22205000000001",
"-4.2290000000000001", "3698.5410000000002", "n.d.", "n.d.",
"998.65449994000028", "0.127", "14.089185000000001", "99.603259739999999",
"0", "0", "191.22834881", "0", "0", "693.60670639000023", "1799.9533043886668",
"1633.1986809599998", "64.704999999999998", "n.d.", "n.d.", "101.98845269866671",
"0", "0", "6.1170730000426943E-2", "10674.840703551334"), `35977` = c("13744.352905719999",
"9314.9829399999999", "602.00114299999996", "1301.0499579999998",
"191.66142134999998", "0", "0", "0", "1109.3885366499999", "3571.6331800000003",
"291.87405825000002", "1041.6974843400001", "2176.7482178400001",
"656.95766700000001", "1187.0872080000001", "182.94085100000001",
"149.76249184", "333.40006599999998", "1538.635389", "593.24546199999997",
"583.62730299999998", "744.16292399999998", "0", "47.227515000000004",
"-8.4049999999999994", "3516.94", "n.d.", "n.d.", "920.83496571999922",
"1.0000000227373675E-8", "1.8897000000000001E-2", "98.918544499999996",
"0", "0", "180.22764240000001", "0", "0", "641.6698818099992",
"2002.1530154710001", "1700.2066716500001", "184.12799999999999",
"n.d.", "n.d.", "96.121409280000051", "0", "0", "21.696934540999948",
"11742.199890249"), `36008` = c("20363.908730990996", "9491.7670990000006",
"532.44559100000004", "1255.758163", "202.99178824999998", "0",
"0", "0", "1052.7663747500001", "3343.0776089999995", "276.18249539999999",
"1022.08258251", "1961.6721877899997", "420.04024099999992",
"1246.2563829999999", "166.29924700000001", "129.07631678999979",
"218.80991200000003", "1778.316544", "730.92296799999997", "944.90770600000008",
"639.83471799999995", "0", "47.693888000000001", "-39.756", "3836.82",
"n.d.", "n.d.", "7075.0776319909965", "5280.5060000000003", "35.319625000000002",
"100.25867321", "0", "0", "196.93138944999998", "0", "0", "1462.0619443309961",
"2349.9023974233332", "2181.8995128999995", "61.953000000000003",
"n.d.", "n.d.", "105.03007437333339", "0", "0", "1.0198101500006658",
"18014.00633356766"), `36039` = c("13435.942530931001", "9043.5347969999984",
"620.245408", "1281.376839", "189.17177395000002", "0", "0",
"0", "1092.20506505", "3407.3325839999998", "280.56836007999999",
"556.81379308999999", "2422.4623104299999", "1027.6085089999999",
"1028.889269", "206.15454600000001", "159.80998642999998", "250.16541800000002",
"1555.7040529999999", "608.96577200000002", "414.17261899999994",
"817.87920399999996", "0", "87.692900000000009", "-90.122", "3656.6120000000001",
"n.d.", "n.d.", "825.91773393100266", "8.9999999999999993E-3",
"1.2554110000000001", "102.92880083000001", "0", "0", "189.64963",
"0", "0", "532.07489210100266", "2293.9131594733331", "2119.3795910200001",
"63.349000000000018", "n.d.", "n.d.", "101.14646933333339", "0",
"0", "10.038099119999572", "11142.029371457671"), `36069` = c("13112.173313491001",
"8605.6747770000002", "581.04622000000006", "1262.7125250000001",
"218.05608486", "0", "0", "0", "1044.6564401400001", "3035.8049339999998",
"111.58819786000001", "1009.1560925299999", "1855.7209905100001",
"712.06523600000003", "815.09957099999997", "207.83347499999999",
"120.72270851000022", "231.51679700000003", "1558.218462", "635.39972299999999",
"602.15865999999994", "632.05329099999994", "0", "66.764164999999991",
"-43.186", "3660.4500000000003", "n.d.", "n.d.", "889.2345364910002",
"1.7999999999999999E-2", "0.73397500000000004", "101.15300791999999",
"0", "0", "192.68519298000001", "0", "0", "594.64436059100012",
"1997.6991413460003", "1815.94290972", "66.417000000000002",
"n.d.", "n.d.", "102.76543625600006", "0", "0", "12.573795370000141",
"11114.474172145001"), `36100` = c("14546.424079400998", "8271.4698059999992",
"552.079069", "1329.2215070000002", "216.73742215999999", "0",
"0", "0", "1112.4840848400002", "2915.1926709999998", "97.490908919999995",
"718.42489679000005", "2049.97595443", "1031.2093519999999",
"788.53030999999999", "116.21842700000001", "114.01786543", "200.31148099999999",
"1542.4363979999998", "618.21667400000001", "393.66905400000007",
"604.80808300000001", "0", "115.534869", "-70.058000000000007",
"3396.9250000000002", "n.d.", "n.d.", "2948.087273400999", "9.9999818101059656E-10",
"673.09786199999996", "95.509254430000027", "0", "0", "201.91575975000001",
"0", "0", "1977.5643972199991", "2088.5287821700003", "1945.0284024399998",
"27.867999999999995", "n.d.", "n.d.", "107.68840520000006", "9.8328539999999993",
"0", "-1.8888794699998801", "12457.895297230998"), `36130` = c("20246.899803710003",
"10819.927981999999", "545.67627000000005", "1307.5069079999998",
"209.52541955999999", "0", "0", "0", "1097.9814884399998", "4679.404368999999",
"109.12242667999999", "729.15418846", "3763.7645197899997", "1888.1224129999998",
"1538.4220789999999", "174.47774799999999", "162.74227978999997",
"244.74447899999998", "1564.0219519999998", "662.73690399999998",
"933.86233600000014", "796.99911500000007", "0", "84.975649000000004",
"-59", "6372.7683211399999", "n.d.", "n.d.", "3113.2035005700018",
"34.838333949999999", "951.95829967999998", "111.91725922000001",
"0", "0", "331.04459944000001", "0", "0", "1683.445008280002",
"2416.2152400913333", "1952.7175891500001", "195.54700000000003",
"n.d.", "n.d.", "176.55711970133345", "58.419996359999999", "0",
"32.973534880000088", "17830.684563618666")), .Names = c("Discriminação",
"35431", "35462", "35490", "35521", "35551", "35582", "35612",
"35643", "35674", "35704", "35735", "35765", "35796", "35827",
"35855", "35886", "35916", "35947", "35977", "36008", "36039",
"36069", "36100", "36130"), row.names = c(NA, -48L), class = c("tbl_df",
"tbl", "data.frame"))
It's because you've told gather to keep the Discriminação column, and also to create a new Discriminação (which will be the column names).
Instead, try:
dat <- gather(data, key = "column_names", value = "Valor", 2:25)

Resources