You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言中按ADR.N.14.0分组实现变量数据行转置的方法问询

Reshaping Data in R to Stack Prefix-2 Variables Under Prefix-1 Variables

Got it, let's break down how to achieve this exact reshaping in R. I'll show you two approaches: one using the tidyverse (great for readable, modern data manipulation) and a base R method if you prefer avoiding external packages.

Tidyverse Approach

First, make sure you have the tidyverse package installed and loaded—it includes dplyr for data manipulation and tidyr for reshaping.

library(tidyverse)

# Define your original data (as provided)
mydat <- structure(list(ADR.N.14.0 = c(8140010250001, 8140010250002), 
                        NOMYAR.N.16.6 = c(1, 1), KOFPOR1.N.16.6 = c(7, 10), 
                        POR1.C.254 = c("о", "BB"), VOZPOR1.N.16.6 = c(80, 45), 
                        VYSPOR1.N.16.6 = c(24, 17), DEMPOR1.N.16.6 = c(36, 16), 
                        POLNOT1.N.16.6 = c(0.6, 0.9), ZAPZAH1.N.16.6 = c(210, 160), 
                        NOMYAR2.N.16.6 = c(1, 1), KOFSOCT2.N.16.6 = c(3, 0), 
                        POR2.C.254 = c("BB", "о"), VOZPOR2.N.16.6 = c(70, 45), 
                        VYSPOR2.N.16.6 = c(22, 17), DEMPOR2.N.16.6 = c(26, 22), 
                        POLNOT2.N.16.6 = c(0, 0), ZAPZAH2.N.16.6 = c(0, 0)), 
                   class = "data.frame", row.names = c(NA, -2L))

# Step 1: Map prefix-2 variable names to their corresponding prefix-1 names
var_mapping <- c(
  "NOMYAR2.N.16.6" = "NOMYAR.N.16.6",
  "KOFSOCT2.N.16.6" = "KOFPOR1.N.16.6",
  "POR2.C.254" = "POR1.C.254",
  "VOZPOR2.N.16.6" = "VOZPOR1.N.16.6",
  "VYSPOR2.N.16.6" = "VYSPOR1.N.16.6",
  "DEMPOR2.N.16.6" = "DEMPOR1.N.16.6",
  "POLNOT2.N.16.6" = "POLNOT1.N.16.6",
  "ZAPZAH2.N.16.6" = "ZAPZAH1.N.16.6"
)

# Step 2: Extract prefix-1 data (including ADR) and prefix-2 data (renamed to match prefix-1)
prefix1_df <- mydat %>% 
  select(ADR.N.14.0, starts_with(c("NOMYAR.", "KOFPOR1", "POR1", "VOZPOR1", "VYSPOR1", "DEMPOR1", "POLNOT1", "ZAPZAH1")))

prefix2_df <- mydat %>% 
  select(ADR.N.14.0, all_of(names(var_mapping))) %>% 
  rename(all_of(var_mapping))

# Step 3: Combine the two datasets and sort by ADR to keep rows grouped
combined_df <- bind_rows(prefix1_df, prefix2_df) %>% 
  arrange(ADR.N.14.0)

# Step 4: Fill to 10 rows, and set empty strings for POR1.C.254 in blank rows (matches your target)
result <- combined_df %>% 
  add_row(.n = 10 - nrow(.)) %>% 
  mutate(POR1.C.254 = ifelse(is.na(ADR.N.14.0), "", POR1.C.254))

# Check the result
print(result)

Base R Approach

If you don't want to use tidyverse, here's an equivalent method using only base R functions:

# Define original data (same as above)
mydat <- structure(list(ADR.N.14.0 = c(8140010250001, 8140010250002), 
                        NOMYAR.N.16.6 = c(1, 1), KOFPOR1.N.16.6 = c(7, 10), 
                        POR1.C.254 = c("о", "BB"), VOZPOR1.N.16.6 = c(80, 45), 
                        VYSPOR1.N.16.6 = c(24, 17), DEMPOR1.N.16.6 = c(36, 16), 
                        POLNOT1.N.16.6 = c(0.6, 0.9), ZAPZAH1.N.16.6 = c(210, 160), 
                        NOMYAR2.N.16.6 = c(1, 1), KOFSOCT2.N.16.6 = c(3, 0), 
                        POR2.C.254 = c("BB", "о"), VOZPOR2.N.16.6 = c(70, 45), 
                        VYSPOR2.N.16.6 = c(22, 17), DEMPOR2.N.16.6 = c(26, 22), 
                        POLNOT2.N.16.6 = c(0, 0), ZAPZAH2.N.16.6 = c(0, 0)), 
                   class = "data.frame", row.names = c(NA, -2L))

# Step 1: Define variable mapping (reverse of tidyverse for base R)
var_mapping <- list(
  NOMYAR.N.16.6 = "NOMYAR2.N.16.6",
  KOFPOR1.N.16.6 = "KOFSOCT2.N.16.6",
  POR1.C.254 = "POR2.C.254",
  VOZPOR1.N.16.6 = "VOZPOR2.N.16.6",
  VYSPOR1.N.16.6 = "VYSPOR2.N.16.6",
  DEMPOR1.N.16.6 = "DEMPOR2.N.16.6",
  POLNOT1.N.16.6 = "POLNOT2.N.16.6",
  ZAPZAH1.N.16.6 = "ZAPZAH2.N.16.6"
)

# Step 2: Extract prefix-1 columns
prefix1_cols <- c("ADR.N.14.0", names(var_mapping))
prefix1_df <- mydat[, prefix1_cols]

# Step 3: Create prefix-2 dataframe with matching column names
prefix2_df <- mydat[, c("ADR.N.14.0", unlist(var_mapping))]
colnames(prefix2_df) <- prefix1_cols

# Step 4: Combine and sort by ADR
combined_df <- rbind(prefix1_df, prefix2_df)
combined_df <- combined_df[order(combined_df$ADR.N.14.0), ]

# Step 5: Fill to 10 rows and adjust POR1.C.254 for blank rows
result <- rbind(combined_df, matrix(NA, nrow = 10 - nrow(combined_df), ncol = ncol(combined_df)))
colnames(result) <- prefix1_cols
result$POR1.C.254[is.na(result$ADR.N.14.0)] <- ""
result <- as.data.frame(result, row.names = 1:10)

# Check the result
print(result)

Both methods will produce exactly the result dataframe you specified, with each ADR.N.14.0 value having its prefix-2 data stacked directly under the prefix-1 data, and filled to 10 rows with appropriate NAs and empty strings.

内容的提问来源于stack exchange,提问作者psysky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:02:30