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

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

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”的名称。

df <- structure(list(state = c("West Bengal", "West Bengal", "West Bengal", 
"West Bengal", "West Bengal"), nn_hospital = c("Khundkuri Hospital", 
"Khundkuri Hospital", "Mankar Rural Hospital", "Khundkuri Hospital", 
"Khundkuri Hospital"), distance_nn_hospital = c(8949.68646563084, 
17217.1419457099, 16939.2318150416, 15812.9872649418, 1408.372117616
), nn_bank = c("contai", "contai", "Allahabad Bank", "contai", 
"contai"), distance_nn_bank = c(13959.9950089655, 20598.4763042432, 
19688.6296071566, 20537.3799009137, 11385.8738290783)), class = "data.frame", row.names = c(NA, 
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.

df&lt;-structure(list(state = c(&quot;West Bengal&quot;, &quot;West Bengal&quot;, &quot;West Bengal&quot;, 
&quot;West Bengal&quot;, &quot;West Bengal&quot;), nn_hospital = c(&quot;Khundkuri Hospital&quot;, 
&quot;Khundkuri Hospital&quot;, &quot;Mankar Rural Hospital&quot;, &quot;Khundkuri Hospital&quot;, 
&quot;Khundkuri Hospital&quot;), distance_nn_hospital = c(8949.68646563084, 
17217.1419457099, 16939.2318150416, 15812.9872649418, 1408.372117616
), nn_bank = c(&quot;contai&quot;, &quot;contai&quot;, &quot;Allahabad Bank&quot;, &quot;contai&quot;, 
&quot;contai&quot;), distance_nn_bank = c(13959.9950089655, 20598.4763042432, 
19688.6296071566, 20537.3799009137, 11385.8738290783)), class = &quot;data.frame&quot;, row.names = c(NA, 
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 来重塑你的数据。之后你需要进行一些额外的清理。

library(tidyr)
library(dplyr, warn = FALSE)
library(stringr)

df |&gt;
  pivot_longer(-state,
    names_to = c(&quot;.value&quot;, &quot;Amenities&quot;),
    names_pattern = &quot;(.*?nn)_(.*)&quot;
  ) |&gt;
  select(-c(state, distance_nn), Name = nn) |&gt;
  mutate(across(c(Amenities, Name), str_to_title))
#&gt; # A tibble: 10 &#215; 2
#&gt;    Amenities Name                 
#&gt;    &lt;chr&gt;     &lt;chr&gt;                
#&gt;  1 Hospital  Khundkuri医院      
#&gt;  2 Bank      Contai               
#&gt;  3 Hospital  Khundkuri医院      
#&gt;  4 Bank      Contai               
#&gt;  5 Hospital  Mankar Rural医院
#&gt;  6 Bank      Allahabad银行     
#&gt;  7 Hospital  Khundkuri医院      
#&gt;  8 Bank      Contai               
#&gt;  9 Hospital  Khundkuri医院      
#&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.

library(tidyr)
library(dplyr, warn = FALSE)
library(stringr)

df |&gt;
  pivot_longer(-state,
    names_to = c(&quot;.value&quot;, &quot;Amenities&quot;),
    names_pattern = &quot;(.*?nn)_(.*)&quot;
  ) |&gt;
  select(-c(state, distance_nn), Name = nn) |&gt;
  mutate(across(c(Amenities, Name), str_to_title))
#&gt; # A tibble: 10 &#215; 2
#&gt;    Amenities Name                 
#&gt;    &lt;chr&gt;     &lt;chr&gt;                
#&gt;  1 Hospital  Khundkuri Hospital   
#&gt;  2 Bank      Contai               
#&gt;  3 Hospital  Khundkuri Hospital   
#&gt;  4 Bank      Contai               
#&gt;  5 Hospital  Mankar Rural Hospital
#&gt;  6 Bank      Allahabad Bank       
#&gt;  7 Hospital  Khundkuri Hospital   
#&gt;  8 Bank      Contai               
#&gt;  9 Hospital  Khundkuri Hospital   
#&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:

确定