是不是可以将两个变量合并成一个?

huangapple go评论100阅读模式
英文:

R: is it possible to merge to 2 variables in one?

问题

你想要在“Nominal Difference”列中加入百分比(带有加号和括号)。以下是你所需的格式:

  1. `City Name` `Average amount` `Nominal Difference` `Real Wage` `Real Difference`
  2. Barletta 2457 1007 (+41.0%) 27.5 11.3
  3. Caserta 2445 910 (+37.2%) 27.4 10.2
  4. Avellino 2363 1016 (+43.0%) 26.5 11.4
  5. Lecce 2342 981 (+41.9%) 26.2 11.0
  6. Benevento 2335 1157 (+49.6%) 26.1 13.0
  7. Isernia 2334 1078 (+46.2%) 26.1 12.1
  8. L'Aquila 2324 1010 (+43.5%) 26.0 11.3
  9. Catanzaro 2310 1533 (+66.3%) 25.9 17.2
  10. Campobasso 2259 1106 (+49.0%) 25.3 12.4
  11. Enna 2242 922 (+41.1%) 25.1 10.3

你可以使用数据处理工具(如Python中的Pandas)来实现这一更改。如果你需要关于具体代码的帮助,请提供你的编程语言和数据结构的信息。

英文:

Is it possible to merge 2 variables in a unique one?

This is my dataset

  1. `City Name` `Average amount` `Nominal Difference` `%` `Real Wage` `Real Difference`
  2. Barletta 2457 1007 41.0 27.5 11.3
  3. Caserta 2445 910 37.2 27.4 10.2
  4. Avellino 2363 1016 43.0 26.5 11.4
  5. Lecce 2342 981 41.9 26.2 11.0
  6. Benevento 2335 1157 49.6 26.1 13.0
  7. Isernia 2334 1078 46.2 26.1 12.1
  8. L'Aquila 2324 1010 43.5 26.0 11.3
  9. Catanzaro 2310 1533 66.3 25.9 17.2
  10. Campobasso 2259 1106 49.0 25.3 12.4
  11. Enna 2242 922 41.1 25.1 10.3

I'd like to have the percentage (with a plus sign and inside parentesis) in the column of Nominal Difference.

To better explain
是不是可以将两个变量合并成一个?

What I'm looking for is

  1. `City Name` `Average amount` `Nominal Difference` `Real Wage` `Real Difference`
  2. Barletta 2457 1007 (+41.0%) 27.5 11.3
  3. Caserta 2445 910 (+37.2%) 27.4 10.2
  4. Avellino 2363 1016 (+43.0%) 26.5 11.4
  5. Lecce 2342 981 (+41.9%) 26.2 11.0
  6. Benevento 2335 1157 (+49.6%) 26.1 13.0
  7. Isernia 2334 1078 (+46.2%) 26.1 12.1
  8. L'Aquila 2324 1010 (+43.5%) 26.0 11.3
  9. Catanzaro 2310 1533 (+66.3%) 25.9 17.2
  10. Campobasso 2259 1106 (+49.0%) 25.3 12.4
  11. Enna 2242 922 (+41.1%) 25.1 10.3

How can I do that?

UPDATE

  1. > dput(f)
  2. structure(list(`City Name` = c("Barletta -Andria-Trani", "Caserta",
  3. "Avellino", "Lecce", "Benevento", "Isernia", "L'Aquila", "Catanzaro",
  4. "Campobasso", "Enna"), `Average amount` = c(2456.92, 2444.58,
  5. 2363.48, 2341.57, 2334.63, 2334.01, 2323.97, 2310.46, 2259.03,
  6. 2242.38), `Nominal Difference` = c(1006.8, 909.62, 1016.28, 980.7,
  7. 1157.25, 1077.51, 1010.32, 1532.79, 1106.31, 922.35), `%` = c(40.97,
  8. 37.2, 42.99, 41.88, 49.56, 46.16, 43.47, 66.34, 48.97, 41.13),
  9. `Real Wage` = c(27.51, 27.37, 26.46, 26.22, 26.14, 26.13,
  10. 26.02, 25.87, 25.29, 25.11), `Real Difference` = c(11.27,
  11. 10.18, 11.38, 10.98, 12.95, 12.06, 11.31, 17.16, 12.38, 10.32
  12. )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
  13. ))

答案1

得分: 2

从我的评论中:

  1. with(dataset, sprintf("%.0f (%+0.1f%%)", `Nominal Difference`, `Real Wage`))
  2. # [1] "1007 (+27.5%)" "910 (+27.4%)" "1016 (+26.5%)" "981 (+26.2%)" "1157 (+26.1%)" "1078 (+26.1%)" "1010 (+26.0%)"
  3. # [8] "1533 (+25.9%)" "1106 (+25.3%)" "922 (+25.1%)"

如果需要对每个子字符串进行对齐,sprintf支持使用前导零进行空格填充,并在左/右填充之间切换:

  1. '-' Left adjustment of converted argument in its field.
  2. '0' For numbers, pad to the field width with leading zeros. For
  3. characters, this zero-pads on some platforms and is ignored
  4. on others.

我不认为我们可以在数字上使用空格填充,但如果我们分别格式化每个数字,我们可以再次在结果字符串上调用sprintf

  1. with(dataset,
  2. sprintf("%-6s %6s", sprintf("%.0f", `Nominal Difference`),
  3. sprintf("(%0+.1f)", `Real Wage`))
  4. )
  5. # [1] "1007 (+27.5)" "910 (+27.4)" "1016 (+26.5)" "981 (+26.2)" "1157 (+26.1)" "1078 (+26.1)" "1010 (+26.0)"
  6. # [8] "1533 (+25.9)" "1106 (+25.3)" "922 (+25.1)"

数据

  1. dataset <- structure(list("City Name" = c("Barletta", "Caserta", "Avellino", "Lecce", "Benevento", "Isernia", "L'Aquila", "Catanzaro", "Campobasso", "Enna"), "Average amount" = c(2457L, 2445L, 2363L, 2342L, 2335L, 2334L, 2324L, 2310L, 2259L, 2242L), "Nominal Difference" = c(1007L, 910L, 1016L, 981L, 1157L, 1078L, 1010L, 1533L, 1106L, 922L), "%" = c(41, 37.2, 43, 41.9, 49.6, 46.2, 43.5, 66.3, 49, 41.1), "Real Wage" = c(27.5, 27.4, 26.5, 26.2, 26.1, 26.1, 26, 25.9, 25.3, 25.1), "Real Difference" = c(11.3, 10.2, 11.4, 11, 13, 12.1, 11.3, 17.2, 12.4, 10.3)), class = "data.frame", row names = c(NA, -10L))
英文:

From my comment:

  1. with(dataset, sprintf(&quot;%.0f (%+0.1f%%)&quot;, `Nominal Difference`, `Real Wage`))
  2. # [1] &quot;1007 (+27.5%)&quot; &quot;910 (+27.4%)&quot; &quot;1016 (+26.5%)&quot; &quot;981 (+26.2%)&quot; &quot;1157 (+26.1%)&quot; &quot;1078 (+26.1%)&quot; &quot;1010 (+26.0%)&quot;
  3. # [8] &quot;1533 (+25.9%)&quot; &quot;1106 (+25.3%)&quot; &quot;922 (+25.1%)&quot;

If you need to align each substring, sprintf supports space-padding with a leading zero, and change between left/right padding with a negation:

  1. &#39;-&#39; Left adjustment of converted argument in its field.
  2. &#39;0&#39; For numbers, pad to the field width with leading zeros. For
  3. characters, this zero-pads on some platforms and is ignored
  4. on others.

I don't think we can use space-padding on a number, but if we format each one individually, we can call sprintf again on the resulting strings.

  1. with(dataset,
  2. sprintf(&quot;%-6s %6s&quot;, sprintf(&quot;%.0f&quot;, `Nominal Difference`),
  3. sprintf(&quot;(%0+.1f)&quot;, `Real Wage`))
  4. )
  5. # [1] &quot;1007 (+27.5)&quot; &quot;910 (+27.4)&quot; &quot;1016 (+26.5)&quot; &quot;981 (+26.2)&quot; &quot;1157 (+26.1)&quot; &quot;1078 (+26.1)&quot; &quot;1010 (+26.0)&quot;
  6. # [8] &quot;1533 (+25.9)&quot; &quot;1106 (+25.3)&quot; &quot;922 (+25.1)&quot;

Data

  1. dataset &lt;- structure(list(&quot;City Name&quot; = c(&quot;Barletta&quot;, &quot;Caserta&quot;, &quot;Avellino&quot;, &quot;Lecce&quot;, &quot;Benevento&quot;, &quot;Isernia&quot;, &quot;L&#39;Aquila&quot;, &quot;Catanzaro&quot;, &quot;Campobasso&quot;, &quot;Enna&quot;), &quot;Average amount&quot; = c(2457L, 2445L, 2363L, 2342L, 2335L, 2334L, 2324L, 2310L, 2259L, 2242L), &quot;Nominal Difference&quot; = c(1007L, 910L, 1016L, 981L, 1157L, 1078L, 1010L, 1533L, 1106L, 922L), &quot;%&quot; = c(41, 37.2, 43, 41.9, 49.6, 46.2, 43.5, 66.3, 49, 41.1), &quot;Real Wage&quot; = c(27.5, 27.4, 26.5, 26.2, 26.1, 26.1, 26, 25.9, 25.3, 25.1), &quot;Real Difference&quot; = c(11.3, 10.2, 11.4, 11, 13, 12.1, 11.3, 17.2, 12.4, 10.3)), class = &quot;data.frame&quot;, row.names = c(NA, -10L))

答案2

得分: 1

我们可以在mutate()内部使用paste0(),并使用sprintf()调整小数位数,使用stringr::str_pad()调整填充:

  1. library(dplyr)
  2. library(stringr)
  3. mydat <- tribble(~ `City Name`, ~ `Nominal Difference`, ~`%`,
  4. "Barletta", 1007, 41.0,
  5. "Caserta", 910, 37.2
  6. )
  7. mydat %>%
  8. mutate(`Nominal Difference` = paste0(
  9. str_pad(
  10. `Nominal Difference`,
  11. width = max(nchar(`Nominal Difference`))),
  12. sprintf(" (+%.1f%%)",`%`))
  13. )
  14. #> # A tibble: 2 x 3
  15. #> `City Name` `Nominal Difference` `%`
  16. #> <chr> <chr> <dbl>
  17. #> 1 Barletta "1007 (+41.0%)" 41
  18. #> 2 Caserta " 910 (+37.2%)" 37.2

创建于2023-02-27,使用 reprex package (v2.0.1)

英文:

We can use paste0() inside mutate() and adjust the decimals with sprintf() and the padding with stringr::str_pad():

  1. library(dplyr)
  2. library(stringr)
  3. mydat &lt;- tribble(~ `City Name`, ~ `Nominal Difference`, ~`%`,
  4. &quot;Barletta&quot;, 1007, 41.0,
  5. &quot;Caserta&quot;, 910, 37.2
  6. )
  7. mydat %&gt;%
  8. mutate(`Nominal Difference` = paste0(
  9. str_pad(
  10. `Nominal Difference`,
  11. width = max(nchar(`Nominal Difference`))),
  12. sprintf(&quot; (+%.1f%%)&quot;,`%`))
  13. )
  14. #&gt; # A tibble: 2 x 3
  15. #&gt; `City Name` `Nominal Difference` `%`
  16. #&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
  17. #&gt; 1 Barletta &quot;1007 (+41.0%)&quot; 41
  18. #&gt; 2 Caserta &quot; 910 (+37.2%)&quot; 37.2

<sup>Created on 2023-02-27 by the reprex package (v2.0.1)</sup>

答案3

得分: 1

我认为你可以使用 unite 函数,例如:

  1. df %>%
  2. mutate(perc = sprintf("(+%.1f%%)", perc)) %>%
  3. unite(val, val:perc, remove = TRUE, sep = " ")

这将输出如下结果:

  1. id val
  2. 1 1 68 (+68.7%)
  3. 2 2 39 (+38.4%)
  4. 3 3 1 (+77.0%)
  5. 4 4 34 (+49.8%)
  6. 5 5 87 (+71.8%)
  7. 6 6 43 (+99.2%)
  8. 7 7 14 (+38.0%)
  9. 8 8 82 (+77.7%)
  10. 9 9 59 (+93.5%)
  11. 10 10 51 (+21.2%)

虚拟数据

  1. set.seed(1)
  2. df <- data.frame(
  3. id = 1:10,
  4. val = sample(100, 10),
  5. perc = 100 * runif(10)
  6. )

希望这对你有帮助。

英文:

I think you can use unite, for example

  1. df %&gt;%
  2. mutate(perc = sprintf(&quot;(+%.1f%%)&quot;, perc)) %&gt;%
  3. unite(val, val:perc, remove = TRUE, sep = &quot; &quot;)

which outputs like

  1. id val
  2. 1 1 68 (+68.7%)
  3. 2 2 39 (+38.4%)
  4. 3 3 1 (+77.0%)
  5. 4 4 34 (+49.8%)
  6. 5 5 87 (+71.8%)
  7. 6 6 43 (+99.2%)
  8. 7 7 14 (+38.0%)
  9. 8 8 82 (+77.7%)
  10. 9 9 59 (+93.5%)
  11. 10 10 51 (+21.2%)

Dummy Data

  1. set.seed(1)
  2. df &lt;- data.frame(
  3. id = 1:10,
  4. val = sample(100, 10),
  5. perc = 100 * runif(10)
  6. )
  7. # &gt; df
  8. # id val perc
  9. # 1 1 68 68.70228
  10. # 2 2 39 38.41037
  11. # 3 3 1 76.98414
  12. # 4 4 34 49.76992
  13. # 5 5 87 71.76185
  14. # 6 6 43 99.19061
  15. # 7 7 14 38.00352
  16. # 8 8 82 77.74452
  17. # 9 9 59 93.47052
  18. # 10 10 51 21.21425

答案4

得分: 1

  1. df %>%
  2. mutate(across(percent, ~ str_c("(", .x, "%)")) %>%
  3. unite("nominal_difference", c(nominal_difference, percent), sep = " ")

A tibble: 10 × 5

city_name average_amount nominal_difference real_wage real_difference

1 Barletta 2457 1007 (41%) 27.5 11.3
2 Caserta 2445 910 (37.2%) 27.4 10.2
3 Avellino 2363 1016 (43%) 26.5 11.4
4 Lecce 2342 981 (41.9%) 26.2 11
5 Benevento 2335 1157 (49.6%) 26.1 13
6 Isernia 2334 1078 (46.2%) 26.1 12.1
7 L'Aquila 2324 1010 (43.5%) 26 11.3
8 Catanzaro 2310 1533 (66.3%) 25.9 17.2
9 Campobasso 2259 1106 (49%) 25.3 12.4
10 Enna 2242 922 (41.1%) 25.1 10.3

  1. <details>
  2. <summary>英文:</summary>
  3. df %&gt;%
  4. mutate(across(percent, ~ str_c(&quot;(&quot;, .x, &quot;%)&quot;))) %&gt;%
  5. unite(&quot;nominal_difference&quot;, c(nominal_difference, percent), sep = &quot; &quot;)
  6. # A tibble: 10 &#215; 5
  7. city_name average_amount nominal_difference real_wage real_difference
  8. &lt;chr&gt; &lt;dbl&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
  9. 1 Barletta 2457 1007 (41%) 27.5 11.3
  10. 2 Caserta 2445 910 (37.2%) 27.4 10.2
  11. 3 Avellino 2363 1016 (43%) 26.5 11.4
  12. 4 Lecce 2342 981 (41.9%) 26.2 11
  13. 5 Benevento 2335 1157 (49.6%) 26.1 13
  14. 6 Isernia 2334 1078 (46.2%) 26.1 12.1
  15. 7 L&#39;Aquila 2324 1010 (43.5%) 26 11.3
  16. 8 Catanzaro 2310 1533 (66.3%) 25.9 17.2
  17. 9 Campobasso 2259 1106 (49%) 25.3 12.4
  18. 10 Enna 2242 922 (41.1%) 25.1 10.3
  19. </details>

huangapple
  • 本文由 发表于 2023年2月27日 18:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定