Column_to_rowname r 中的函数
Column_to_rowname function in r
我遇到了一个问题:
使用相同的数据集,以不同的方式导入,在使用 column_to_rowname
函数后有不同的结果:
当我从 csv
格式导入数据集时:
library(tibble)
data <- read.table(text = data, sep = ",", header = TRUE, stringsAsFactors = FALSE)
data <- as.data.frame(data)
$ Contas.Resultado <chr> "% C.M.V.", "% Despesas Financeiras", "% Despesas Operacionais", "% Despesas Pessoal", "% Despesa...
$ `Foz do Iguaçu` <dbl> 6.861259e-01, 4.351738e-03, 1.948886e-02, 5.030020e-02, NA, 5.405629e-03, 3.937407e-03, 1.582344e...
$ `Galinha Caipira` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9.556879e+06, 1.144605e+06, 3.651308e+05, 9.2...
$ `Indústria de Aves` <dbl> 6.996671e-01, 6.270924e-04, 1.519735e-01, 2.824831e-02, 5.424471e-03, 1.509008e-02, 1.940629e-02,...
$ `Postos de Gasolina` <dbl> 7.137772e-01, 1.390428e-03, 3.290988e-02, 4.004685e-02, 1.485064e-02, 3.844833e-03, 2.604383e-02,...
$ `São Paulo` <dbl> 5.394683e-01, NA, NA, NA, NA, NA, NA, 1.804614e-02, 2.331499e-03, NA, NA, 2.207766e+07, NA, NA, N...
$ `Total das Áreas de Negócios` <dbl> 6.656677e-01, 3.846783e-02, 5.555819e-02, 4.171821e-02, 9.965640e-03, 1.030095e-02, 2.735231e-02,...
$ `Unidade MS` <dbl> 6.230891e-01, 1.412913e-03, 3.335100e-02, 2.960971e-02, 1.512218e-02, 4.711866e-03, 2.085070e-02,...
$ `Unidade RS` <dbl> 8.374991e-01, 1.359509e-03, 3.230330e-02, 5.439893e-02, 1.447725e-02, 2.652581e-03, 3.318487e-02,...
并将第一列传递给行名,一切正常:
data <- data %>% remove_rownames %>% column_to_rownames(var = "Contas.Resultado")
我的问题是:
真正的数据集是一个 mdx api 查询(从 json
转换为 dataframe,然后是 cbind
函数),而不是 csv
。当我导入数据时,我尝试创建一个非常相似的格式:
Data1 <- as.data.frame(Data1)
$ `Contas Resultado` <fct> % C.M.V., % Despesas Financeiras, % Despesas Operacionais, % Despesas Pesso...
$ `Foz do Iguaçu` <dbl> 6.861259e-01, 4.351738e-03, 1.948886e-02, 5.030020e-02, NA, 5.405629e-03, 3...
$ `Galinha Caipira` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9.556879e+06, 1.144605e...
$ `Indústria de Aves` <dbl> 6.996671e-01, 6.270924e-04, 1.519735e-01, 2.824831e-02, 5.424471e-03, 1.509...
$ `Postos de Gasolina` <dbl> 7.137772e-01, 1.390428e-03, 3.290988e-02, 4.004685e-02, 1.485064e-02, 3.844...
$ `São Paulo` <dbl> 5.394683e-01, NA, NA, NA, NA, NA, NA, 1.804614e-02, 2.331499e-03, NA, NA, 2...
$ `Total das Áreas de Negócios` <dbl> 6.656677e-01, 3.846783e-02, 5.555819e-02, 4.171821e-02, 9.965640e-03, 1.030...
$ `Unidade MS` <dbl> 6.230891e-01, 1.412913e-03, 3.335100e-02, 2.960971e-02, 1.512218e-02, 4.711...
$ `Unidade RS` <dbl> 8.374991e-01, 1.359509e-03, 3.230330e-02, 5.439893e-02, 1.447725e-02, 2.652...
当我尝试执行相同的过程时:
Data1 <- Data1 %>% remove_rownames %>% column_to_rownames(var="`Contas Resultado`")
我收到一条错误消息:
Error: Column `num2` not found
Dput
共 Data1
structure(list(`Contas Resultado` = structure(1:32, .Label = c("% C.M.V.",
"% Despesas Financeiras", "% Despesas Operacionais", "% Despesas Pessoal",
"% Despesas Pró-labore", "% Despesas Tributárias", "% Despesas Vendas",
"% Devoluções", "% Impostos", "% Receitas Financeiras", "% Receitas Operacionais",
"C.M.V.", "Despesas Financeiras", "Despesas Operacionais", "Despesas Pessoal",
"Despesas Pró-labore", "Despesas Tributárias", "Despesas Vendas",
"Devoluções", "EBTI", "EBTIDA R$", "Faturamento", "Impostos",
"Margem Bruta", "Margem Ebtida", "Margem Líquida", "Receita Líquida",
"Receitas Financeiras", "Receitas Operacionais", "Resultado Bruto",
"Resultado Líquido", "ROE"), class = "factor"), `Foz do Iguaçu` = c(0.68612585544488,
0.0043517384317626, 0.0194888625807808, 0.0503002002622742, NA,
0.00540562915716222, 0.00393740650270395, 0.0158234413058695,
0.00298555558943327, 0.00309379115135203, 0.00519697211916096,
11890516.6296312, 76861.0238892454, 344215.06620491, 888409.30001084,
NA, 95474.9919601125, 69543.03435545, 279475.873674674, 4133565.86239994,
4133565.86239994, 17662142.4045733, 52731.3079773401, 0.31387414455512,
0.238521714549327, 0.237239652976778, 17329935.2229212, 54642.9798851882,
91789.6616412177, 5439418.59329004, 4111347.81839589, 0.00292941908441637
), `Galinha Caipira` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 9556878.92418847, 1144604.93814171, 365130.776145752,
925127.444386077, 1135558.62804292, NA, -12452272.7712085, -12452272.7712085,
NA, NA, NA, NA, NA, NA, 231098.009820487, 675027.93969646, NA,
-12221174.761388, -0.00870783600935179), `Indústria de Aves` = c(0.699667052082928,
0.000627092426749223, 0.151973469590285, 0.0282483062516762,
0.0054244708112611, 0.0150900776642944, 0.0194062924371954, 0.016149713612494,
0.0441365437855457, 0.00474956854966594, 0.0219427540766736,
44256601.893048, 42210.7063455348, 10229604.478628, 1901443.72517686,
365130.776145752, 1015739.96088709, 1306272.05238058, 1087065.94081398,
5656015.9308692, 5659151.43204849, 67311778.2084378, 2970909.24617965,
0.300332947917072, 0.0894673705252147, 0.093804749215047, 63253803.0214441,
319701.904800885, 1477005.79569135, 18997201.1283961, 5933507.12932455,
0.0042277447177762), `Postos de Gasolina` = c(0.713777232261634,
0.00139042807966732, 0.0329098763968368, 0.0400468547401319,
0.0148506386870228, 0.00384483346282912, 0.0260438268929179,
0.0157740600011077, 0.00456634697984958, 0.00816671484651574,
0.00123371381293295, 58692862.9049296, 116706.739805739, 2762317.90615421,
3361366.1321577, 1246500.73638479, 322719.301418422, 2186010.31810629,
1324008.8740655, 13760319.9963958, 14644840.3358183, 83935833.5122681,
383280.139859902, 0.286222767738367, 0.178099228499824, 0.1742593567928,
82228544.4983427, 685480.017699313, 103552.797204126, 23535681.5934131,
14329093.2742893, 0.0102097708961202), `São Paulo` = c(0.539468287243312,
NA, NA, NA, NA, NA, NA, 0.018046138403897, 0.00233149930616966,
NA, NA, 22077662.0410456, NA, NA, NA, NA, NA, NA, 753898.283945763,
18847194.0869442, 18847194.0869442, 41776155.4894737, 97401.0775381438,
0.460531712756688, 0.460531712756689, 0.460531712756689, 40924856.1279898,
NA, NA, 18847194.0869442, 18847194.0869442, 0.0134290097760533
), `Total das Áreas de Negócios` = c(0.665667747374264, 0.0384678251694839,
0.0555581900068015, 0.0417182114705366, 0.00996563959620955,
0.0103009450485867, 0.0273523085436607, 0.0159912681090607, 0.018972970689887,
0.0323679824248229, 0.0303066463387605, 177825304.573185, 10648545.0440942,
15379447.2718227, 11548306.9824147, 2758657.70071218, 2851475.92470683,
7571581.92083575, 4426653.65201372, 57592849.407689, 58533637.2998705,
276816924.200374, 5252039.38931835, 0.334332252625736, 0.219113666531027,
0.209271093315442, 267138231.159041, 8960005.33741123, 8389392.62232419,
89312926.5858569, 55904309.7010059, 0.0398329596456148), `Unidade MS` = c(0.623089149597576,
0.00141291279236638, 0.0333509956937994, 0.0296097093525557,
0.0151221778944891, 0.00471186576819375, 0.0208507035043875,
0.0157192326945189, 0.00788709493679481, 0.0129396633998504,
NA, 29564762.3727304, 68661.7083993295, 1620720.22670253, 1438909.20963995,
734875.19864209, 228977.156368338, 1013257.81157475, 763889.588489469,
12847185.5706182, 12847185.5706182, 48595857.2746256, 383280.139859902,
0.376910850402424, 0.270759555953755, 0.282564989493201, 47448687.5462762,
628814.035760828, NA, 17883925.1735459, 13407337.8979797, 0.00955300141083288
), `Unidade RS` = c(0.837499147059275, 0.00135950944288507, 0.0323032950496358,
0.054398930819598, 0.0144772462296604, 0.00265258087384434, 0.0331848696967253,
0.0158494528069155, NA, 0.00160345274590558, 0.00293018864833944,
29128100.5321992, 48045.0314064091, 1141597.67945168, 1922456.92251775,
511625.537742699, 93742.145050084, 1172752.50653153, 560119.28557603,
913134.425777582, 1797654.76520007, 35339976.2376425, NA, 0.162500852940725,
0.051686663567294, 0.0265025637563726, 34779856.9520664, 56665.9819384857,
103552.797204126, 5651756.4198672, 921755.376309658, 0.000656769485287294
)), .Names = c("Contas Resultado", "Foz do Iguaçu", "Galinha Caipira",
"Indústria de Aves", "Postos de Gasolina", "São Paulo", "Total das Áreas de Negócios",
"Unidade MS", "Unidade RS"), class = "data.frame", row.names = c(NA,
-32L))
基本 R 函数问题的最简单解决方案如下所示:
rownames(Data1) <- Data1[, 1]
Data1 <- Data1[, -1]
我遇到了一个问题:
使用相同的数据集,以不同的方式导入,在使用 column_to_rowname
函数后有不同的结果:
当我从 csv
格式导入数据集时:
library(tibble)
data <- read.table(text = data, sep = ",", header = TRUE, stringsAsFactors = FALSE)
data <- as.data.frame(data)
$ Contas.Resultado <chr> "% C.M.V.", "% Despesas Financeiras", "% Despesas Operacionais", "% Despesas Pessoal", "% Despesa...
$ `Foz do Iguaçu` <dbl> 6.861259e-01, 4.351738e-03, 1.948886e-02, 5.030020e-02, NA, 5.405629e-03, 3.937407e-03, 1.582344e...
$ `Galinha Caipira` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9.556879e+06, 1.144605e+06, 3.651308e+05, 9.2...
$ `Indústria de Aves` <dbl> 6.996671e-01, 6.270924e-04, 1.519735e-01, 2.824831e-02, 5.424471e-03, 1.509008e-02, 1.940629e-02,...
$ `Postos de Gasolina` <dbl> 7.137772e-01, 1.390428e-03, 3.290988e-02, 4.004685e-02, 1.485064e-02, 3.844833e-03, 2.604383e-02,...
$ `São Paulo` <dbl> 5.394683e-01, NA, NA, NA, NA, NA, NA, 1.804614e-02, 2.331499e-03, NA, NA, 2.207766e+07, NA, NA, N...
$ `Total das Áreas de Negócios` <dbl> 6.656677e-01, 3.846783e-02, 5.555819e-02, 4.171821e-02, 9.965640e-03, 1.030095e-02, 2.735231e-02,...
$ `Unidade MS` <dbl> 6.230891e-01, 1.412913e-03, 3.335100e-02, 2.960971e-02, 1.512218e-02, 4.711866e-03, 2.085070e-02,...
$ `Unidade RS` <dbl> 8.374991e-01, 1.359509e-03, 3.230330e-02, 5.439893e-02, 1.447725e-02, 2.652581e-03, 3.318487e-02,...
并将第一列传递给行名,一切正常:
data <- data %>% remove_rownames %>% column_to_rownames(var = "Contas.Resultado")
我的问题是:
真正的数据集是一个 mdx api 查询(从 json
转换为 dataframe,然后是 cbind
函数),而不是 csv
。当我导入数据时,我尝试创建一个非常相似的格式:
Data1 <- as.data.frame(Data1)
$ `Contas Resultado` <fct> % C.M.V., % Despesas Financeiras, % Despesas Operacionais, % Despesas Pesso...
$ `Foz do Iguaçu` <dbl> 6.861259e-01, 4.351738e-03, 1.948886e-02, 5.030020e-02, NA, 5.405629e-03, 3...
$ `Galinha Caipira` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9.556879e+06, 1.144605e...
$ `Indústria de Aves` <dbl> 6.996671e-01, 6.270924e-04, 1.519735e-01, 2.824831e-02, 5.424471e-03, 1.509...
$ `Postos de Gasolina` <dbl> 7.137772e-01, 1.390428e-03, 3.290988e-02, 4.004685e-02, 1.485064e-02, 3.844...
$ `São Paulo` <dbl> 5.394683e-01, NA, NA, NA, NA, NA, NA, 1.804614e-02, 2.331499e-03, NA, NA, 2...
$ `Total das Áreas de Negócios` <dbl> 6.656677e-01, 3.846783e-02, 5.555819e-02, 4.171821e-02, 9.965640e-03, 1.030...
$ `Unidade MS` <dbl> 6.230891e-01, 1.412913e-03, 3.335100e-02, 2.960971e-02, 1.512218e-02, 4.711...
$ `Unidade RS` <dbl> 8.374991e-01, 1.359509e-03, 3.230330e-02, 5.439893e-02, 1.447725e-02, 2.652...
当我尝试执行相同的过程时:
Data1 <- Data1 %>% remove_rownames %>% column_to_rownames(var="`Contas Resultado`")
我收到一条错误消息:
Error: Column `num2` not found
Dput
共 Data1
structure(list(`Contas Resultado` = structure(1:32, .Label = c("% C.M.V.",
"% Despesas Financeiras", "% Despesas Operacionais", "% Despesas Pessoal",
"% Despesas Pró-labore", "% Despesas Tributárias", "% Despesas Vendas",
"% Devoluções", "% Impostos", "% Receitas Financeiras", "% Receitas Operacionais",
"C.M.V.", "Despesas Financeiras", "Despesas Operacionais", "Despesas Pessoal",
"Despesas Pró-labore", "Despesas Tributárias", "Despesas Vendas",
"Devoluções", "EBTI", "EBTIDA R$", "Faturamento", "Impostos",
"Margem Bruta", "Margem Ebtida", "Margem Líquida", "Receita Líquida",
"Receitas Financeiras", "Receitas Operacionais", "Resultado Bruto",
"Resultado Líquido", "ROE"), class = "factor"), `Foz do Iguaçu` = c(0.68612585544488,
0.0043517384317626, 0.0194888625807808, 0.0503002002622742, NA,
0.00540562915716222, 0.00393740650270395, 0.0158234413058695,
0.00298555558943327, 0.00309379115135203, 0.00519697211916096,
11890516.6296312, 76861.0238892454, 344215.06620491, 888409.30001084,
NA, 95474.9919601125, 69543.03435545, 279475.873674674, 4133565.86239994,
4133565.86239994, 17662142.4045733, 52731.3079773401, 0.31387414455512,
0.238521714549327, 0.237239652976778, 17329935.2229212, 54642.9798851882,
91789.6616412177, 5439418.59329004, 4111347.81839589, 0.00292941908441637
), `Galinha Caipira` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 9556878.92418847, 1144604.93814171, 365130.776145752,
925127.444386077, 1135558.62804292, NA, -12452272.7712085, -12452272.7712085,
NA, NA, NA, NA, NA, NA, 231098.009820487, 675027.93969646, NA,
-12221174.761388, -0.00870783600935179), `Indústria de Aves` = c(0.699667052082928,
0.000627092426749223, 0.151973469590285, 0.0282483062516762,
0.0054244708112611, 0.0150900776642944, 0.0194062924371954, 0.016149713612494,
0.0441365437855457, 0.00474956854966594, 0.0219427540766736,
44256601.893048, 42210.7063455348, 10229604.478628, 1901443.72517686,
365130.776145752, 1015739.96088709, 1306272.05238058, 1087065.94081398,
5656015.9308692, 5659151.43204849, 67311778.2084378, 2970909.24617965,
0.300332947917072, 0.0894673705252147, 0.093804749215047, 63253803.0214441,
319701.904800885, 1477005.79569135, 18997201.1283961, 5933507.12932455,
0.0042277447177762), `Postos de Gasolina` = c(0.713777232261634,
0.00139042807966732, 0.0329098763968368, 0.0400468547401319,
0.0148506386870228, 0.00384483346282912, 0.0260438268929179,
0.0157740600011077, 0.00456634697984958, 0.00816671484651574,
0.00123371381293295, 58692862.9049296, 116706.739805739, 2762317.90615421,
3361366.1321577, 1246500.73638479, 322719.301418422, 2186010.31810629,
1324008.8740655, 13760319.9963958, 14644840.3358183, 83935833.5122681,
383280.139859902, 0.286222767738367, 0.178099228499824, 0.1742593567928,
82228544.4983427, 685480.017699313, 103552.797204126, 23535681.5934131,
14329093.2742893, 0.0102097708961202), `São Paulo` = c(0.539468287243312,
NA, NA, NA, NA, NA, NA, 0.018046138403897, 0.00233149930616966,
NA, NA, 22077662.0410456, NA, NA, NA, NA, NA, NA, 753898.283945763,
18847194.0869442, 18847194.0869442, 41776155.4894737, 97401.0775381438,
0.460531712756688, 0.460531712756689, 0.460531712756689, 40924856.1279898,
NA, NA, 18847194.0869442, 18847194.0869442, 0.0134290097760533
), `Total das Áreas de Negócios` = c(0.665667747374264, 0.0384678251694839,
0.0555581900068015, 0.0417182114705366, 0.00996563959620955,
0.0103009450485867, 0.0273523085436607, 0.0159912681090607, 0.018972970689887,
0.0323679824248229, 0.0303066463387605, 177825304.573185, 10648545.0440942,
15379447.2718227, 11548306.9824147, 2758657.70071218, 2851475.92470683,
7571581.92083575, 4426653.65201372, 57592849.407689, 58533637.2998705,
276816924.200374, 5252039.38931835, 0.334332252625736, 0.219113666531027,
0.209271093315442, 267138231.159041, 8960005.33741123, 8389392.62232419,
89312926.5858569, 55904309.7010059, 0.0398329596456148), `Unidade MS` = c(0.623089149597576,
0.00141291279236638, 0.0333509956937994, 0.0296097093525557,
0.0151221778944891, 0.00471186576819375, 0.0208507035043875,
0.0157192326945189, 0.00788709493679481, 0.0129396633998504,
NA, 29564762.3727304, 68661.7083993295, 1620720.22670253, 1438909.20963995,
734875.19864209, 228977.156368338, 1013257.81157475, 763889.588489469,
12847185.5706182, 12847185.5706182, 48595857.2746256, 383280.139859902,
0.376910850402424, 0.270759555953755, 0.282564989493201, 47448687.5462762,
628814.035760828, NA, 17883925.1735459, 13407337.8979797, 0.00955300141083288
), `Unidade RS` = c(0.837499147059275, 0.00135950944288507, 0.0323032950496358,
0.054398930819598, 0.0144772462296604, 0.00265258087384434, 0.0331848696967253,
0.0158494528069155, NA, 0.00160345274590558, 0.00293018864833944,
29128100.5321992, 48045.0314064091, 1141597.67945168, 1922456.92251775,
511625.537742699, 93742.145050084, 1172752.50653153, 560119.28557603,
913134.425777582, 1797654.76520007, 35339976.2376425, NA, 0.162500852940725,
0.051686663567294, 0.0265025637563726, 34779856.9520664, 56665.9819384857,
103552.797204126, 5651756.4198672, 921755.376309658, 0.000656769485287294
)), .Names = c("Contas Resultado", "Foz do Iguaçu", "Galinha Caipira",
"Indústria de Aves", "Postos de Gasolina", "São Paulo", "Total das Áreas de Negócios",
"Unidade MS", "Unidade RS"), class = "data.frame", row.names = c(NA,
-32L))
基本 R 函数问题的最简单解决方案如下所示:
rownames(Data1) <- Data1[, 1]
Data1 <- Data1[, -1]