Create a new column in the dataframe using the column names as its values.

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

Use column names from dataframe and create a new one with one column with those column names as values

问题

我有以下数据框,我想创建一个新的数据框,其中包含两列,一列名为“设施”(Amenities),其值将包括“nn_bank”或“nn_hospital”,另一列名为“名称”(Name),其值将是“nn_bank”或“nn_hospital”的名称。

  1. df <- structure(list(state = c("West Bengal", "West Bengal", "West Bengal",
  2. "West Bengal", "West Bengal"), nn_hospital = c("Khundkuri Hospital",
  3. "Khundkuri Hospital", "Mankar Rural Hospital", "Khundkuri Hospital",
  4. "Khundkuri Hospital"), distance_nn_hospital = c(8949.68646563084,
  5. 17217.1419457099, 16939.2318150416, 15812.9872649418, 1408.372117616
  6. ), nn_bank = c("contai", "contai", "Allahabad Bank", "contai",
  7. "contai"), distance_nn_bank = c(13959.9950089655, 20598.4763042432,
  8. 19688.6296071566, 20537.3799009137, 11385.8738290783)), class = "data.frame", row.names = c(NA,
  9. 5L))

结果应该如下:

Create a new column in the dataframe using the column names as its values.

英文:

I have the dataframe below and I want to create a new one with 2 columns one named Amenities which will include either "nn_bank" or "nn_hospital" as value and the other column "Name" with the name of the nn_bank or nn_hospital.

  1. df&lt;-structure(list(state = c(&quot;West Bengal&quot;, &quot;West Bengal&quot;, &quot;West Bengal&quot;,
  2. &quot;West Bengal&quot;, &quot;West Bengal&quot;), nn_hospital = c(&quot;Khundkuri Hospital&quot;,
  3. &quot;Khundkuri Hospital&quot;, &quot;Mankar Rural Hospital&quot;, &quot;Khundkuri Hospital&quot;,
  4. &quot;Khundkuri Hospital&quot;), distance_nn_hospital = c(8949.68646563084,
  5. 17217.1419457099, 16939.2318150416, 15812.9872649418, 1408.372117616
  6. ), nn_bank = c(&quot;contai&quot;, &quot;contai&quot;, &quot;Allahabad Bank&quot;, &quot;contai&quot;,
  7. &quot;contai&quot;), distance_nn_bank = c(13959.9950089655, 20598.4763042432,
  8. 19688.6296071566, 20537.3799009137, 11385.8738290783)), class = &quot;data.frame&quot;, row.names = c(NA,
  9. 5L))

result should be like

Create a new column in the dataframe using the column names as its values.

答案1

得分: 1

Here is the translated content:

当你有多个值列时,一种选择是使用 tidyr::pivot_longernames_pattern 参数以及特殊的 .value 来重塑你的数据。之后你需要进行一些额外的清理。

  1. library(tidyr)
  2. library(dplyr, warn = FALSE)
  3. library(stringr)
  4. df |&gt;
  5. pivot_longer(-state,
  6. names_to = c(&quot;.value&quot;, &quot;Amenities&quot;),
  7. names_pattern = &quot;(.*?nn)_(.*)&quot;
  8. ) |&gt;
  9. select(-c(state, distance_nn), Name = nn) |&gt;
  10. mutate(across(c(Amenities, Name), str_to_title))
  11. #&gt; # A tibble: 10 &#215; 2
  12. #&gt; Amenities Name
  13. #&gt; &lt;chr&gt; &lt;chr&gt;
  14. #&gt; 1 Hospital Khundkuri医院
  15. #&gt; 2 Bank Contai
  16. #&gt; 3 Hospital Khundkuri医院
  17. #&gt; 4 Bank Contai
  18. #&gt; 5 Hospital Mankar Rural医院
  19. #&gt; 6 Bank Allahabad银行
  20. #&gt; 7 Hospital Khundkuri医院
  21. #&gt; 8 Bank Contai
  22. #&gt; 9 Hospital Khundkuri医院
  23. #&gt; 10 Bank Contai

(Note: I have left the code part unchanged as per your request.)

英文:

When you have multiple value columns one option would be to use the names_pattern argument of tidyr::pivot_longer along with the special .value to reshape your data. Afterwards you have to do some additional cleaning.

  1. library(tidyr)
  2. library(dplyr, warn = FALSE)
  3. library(stringr)
  4. df |&gt;
  5. pivot_longer(-state,
  6. names_to = c(&quot;.value&quot;, &quot;Amenities&quot;),
  7. names_pattern = &quot;(.*?nn)_(.*)&quot;
  8. ) |&gt;
  9. select(-c(state, distance_nn), Name = nn) |&gt;
  10. mutate(across(c(Amenities, Name), str_to_title))
  11. #&gt; # A tibble: 10 &#215; 2
  12. #&gt; Amenities Name
  13. #&gt; &lt;chr&gt; &lt;chr&gt;
  14. #&gt; 1 Hospital Khundkuri Hospital
  15. #&gt; 2 Bank Contai
  16. #&gt; 3 Hospital Khundkuri Hospital
  17. #&gt; 4 Bank Contai
  18. #&gt; 5 Hospital Mankar Rural Hospital
  19. #&gt; 6 Bank Allahabad Bank
  20. #&gt; 7 Hospital Khundkuri Hospital
  21. #&gt; 8 Bank Contai
  22. #&gt; 9 Hospital Khundkuri Hospital
  23. #&gt; 10 Bank Contai

huangapple
  • 本文由 发表于 2023年6月12日 22:16:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457551.html
匿名

发表评论

匿名网友

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

确定