r使用字符串重塑宽至长

发布于 2025-01-30 18:29:46 字数 1233 浏览 3 评论 0原文

我有一个表格:

cou自己的indaus_d_ause_f_b
ausda3268.0285.087
ausdb92.132910808.3

,我想将其重塑为长键入数据,如下所示,表B:表B:

cou2a aindcou2ind cou2 ind2ind2value
ind2ind2ind2a aus aus aus aus aus aus aus aus aus aus aus aus aus aus aus aus ausDA3268.02
AUSDB AUSDA92.1329AUS
DAUSAFB85.087AUS
DBUSA F USA F USA F BUSAFB10808.3

,但我不知道如何使用R或Stata对其进行编码?任何人都可以帮助我,非常感谢

PS:数据只是一个示例,实际上我有数千列(三个维度:country_ownhips_industry,例如AUS_D_C21),60个国家 /地区,2个所有权,34个行业,所以我有4080个COLS。

I have a table A like this:

couownindaus_d_ausa_f_b
AUSDA3268.0285.087
AUSDB92.132910808.3

and I want to reshape it to long type data as follows, table B:

couownindcou2own2ind2value
AUSDAausda3268.02
AUSDBausda92.1329
AUSDAusafb85.087
AUSDBusafb10808.3

but I don't know how to code it using R or Stata? anyone can help me, thanks a lot

PS: the data is just a sample, actually I have thousands of columns (three dimensions: country_ownership_industry, eg aus_d_c21), 60 countries, 2 ownership, 34 industries, so I have 4080 cols.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2025-02-06 18:29:46

只要您的country_ownhips_industry列是其列名中下划线的唯一列,您可以做:

library(tidyr)
library(dplyr)

pivot_longer(df, contains('_'), names_sep = '_', names_to = c('cou2', 'own2', 'ind2'))
#> # A tibble: 4 x 7
#>   cou   own   ind   cou2  own2  ind2    value
#>   <chr> <chr> <chr> <chr> <chr> <chr>   <dbl>
#> 1 AUS   D     A     aus   d     a      3268. 
#> 2 AUS   D     A     usa   f     b        85.1
#> 3 AUS   D     B     aus   d     a        92.1
#> 4 AUS   D     B     usa   f     b     10808. 

Provided your country_ownership_industry columns are the only ones with underscores in their column names, you can do:

library(tidyr)
library(dplyr)

pivot_longer(df, contains('_'), names_sep = '_', names_to = c('cou2', 'own2', 'ind2'))
#> # A tibble: 4 x 7
#>   cou   own   ind   cou2  own2  ind2    value
#>   <chr> <chr> <chr> <chr> <chr> <chr>   <dbl>
#> 1 AUS   D     A     aus   d     a      3268. 
#> 2 AUS   D     A     usa   f     b        85.1
#> 3 AUS   D     B     aus   d     a        92.1
#> 4 AUS   D     B     usa   f     b     10808. 
不寐倦长更 2025-02-06 18:29:46

使用pivot_longer独立的组合:

# using your own example data
dat1 <- tibble(
  cou = c('AUS', 'AUS'), 
  own = c('D', 'D'), 
  ind = c('A', 'B'), 
  aus_d_a = c(3268.02, 92.1329), 
  usa_f_b = c(85.087, 10808.3)
)

library(tidyverse)

dat1 %>%
  pivot_longer(cols = aus_d_a:usa_f_b, names_to = 'cou2', values_to = 'value') %>%
  separate(cou2, c('cou2', 'own2', 'ind2'), sep = '_')

Using a combination of pivot_longer and separate:

# using your own example data
dat1 <- tibble(
  cou = c('AUS', 'AUS'), 
  own = c('D', 'D'), 
  ind = c('A', 'B'), 
  aus_d_a = c(3268.02, 92.1329), 
  usa_f_b = c(85.087, 10808.3)
)

library(tidyverse)

dat1 %>%
  pivot_longer(cols = aus_d_a:usa_f_b, names_to = 'cou2', values_to = 'value') %>%
  separate(cou2, c('cou2', 'own2', 'ind2'), sep = '_')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文