简体   繁体   English

将dataframe1转换为dataframe2,其中列的值成为新列

[英]Transforming dataframe1 to dataframe2 where the values of a column become new columns

I am working with dataframes in R and I would like to change the format of table 1 (image1), to the table 2 (image2).我正在使用 R 中的数据帧,我想将表 1(image1)的格式更改为表 2(image2)。

I know that i have 50 'City' possible values, from 1 to 50. What i want is that those values of 'City' become different columns, so below each of these values the number of 'House' is shown.我知道我有 50 个“城市”可能的值,从 1 到 50。我想要的是“城市”的这些值成为不同的列,因此在每个这些值下方显示“房屋”的数量。 Finally i would have only one row for every Country, and 52 columns.最后,每个国家/地区只有一行,52 列。 ('Number','Country',1,2,3...,50). ('数字','国家',1,2,3...,50)。 (This is a simplify version, the main one has much more values of 'Number', so i am looking for a solution that works 'fast', that does not take long for running) (这是一个简化版本,主要版本有更多的“数字”值,所以我正在寻找一种“快速”工作的解决方案,运行时间不长)

在此处输入图像描述

在此处输入图像描述

If df is your dataframe and columns are lowercase the following dplyr::pivot_wider will help you.如果df是您的 dataframe 并且列是小写的,则以下 dplyr::pivot_wider 将为您提供帮助。

library(dplyr)
df %>% 
  pivot_wider(id_cols=c(number,country),names_from=city,values_from=house)

To give you this output.给你这个output。

# A tibble: 3 × 7
  number country `21`  `23`  `24`  `22`  `25` 
   <int>   <int> <chr> <chr> <chr> <chr> <chr>
1   6205       2 2     a     5     NA    NA   
2   6205       3 NA    5     5     1     5    
3   6205       4 4     2     7     3     2    
> 

Its advisable to not use numeric dataframe column names, hence ideal to re-look at your naming conventions.建议不要使用数字 dataframe 列名,因此非常适合重新查看您的命名约定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从R中的dataframe1中选择dataframe $ 1column的行中选择行 - how to select rows from a dataframe1 in R where dataframe$1column is found somewhere in dataframe2$column 根据dataframe2的列更新dataframe1的列,如果column1不为空,则创建新行 - Update column of dataframe1 based on column of dataframe2 + create new row if column1 is not empty R循环查看dataframe1中的列值是否与dataframe2中的列值匹配 - R loop to see if column value(s) from dataframe1 match column values from dataframe2 根据两个R中的匹配列从column2(dataframe2)中减去column1(dataframe1) - subtract column1 (dataframe1) from column2 (dataframe2) based on matching column in both R 如何根据dataframe1中的值从dataframe2子集并将所有子集堆叠到R中的一个数据帧中? - How to subset from dataframe2 depending on the values in dataframe1 and stack all subsets in one dataframe in R? R:根据dataframe2中相应的单元格值更新dataframe1中的单元格值 - R: updating cell values in dataframe1 based on corresponding cell values in dataframe2 验证Dataframe2中是否存在Dataframe1的所有行 - Verify all rows of Dataframe1 are present in Dataframe2 如何编写for循环,以从dataframe1抓取数据并将其添加到dataframe2? - How can I code a for loop that grabs data from dataframe1 and adds it to dataframe2? 根据特定条件与dataframe2匹配的有效方式来更新dataframe1中的特定行 - Efficient way to update specific row in dataframe1 based on specific condition match with that of dataframe2 数据框,按列值拆分并放入新列 - Dataframe, split by column values and put into new columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM