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

R语言:按ID将多行唯一Att值转换为多列的实现方法

Reshape Long Data Frame to Wide Format with Dynamic Columns in R

Hey there! I totally get how confusing data reshaping can feel when you're new to R—let's break this down step by step so you can get your data into that desired wide format easily.

First, let's start with your sample data so we can work with something concrete:

# Create your example data frame
df <- data.frame(
  ID = c(1, 1, 1, 2, 3, 3, 4),
  Att = c("a", "b", "c", "d", "e", "f", "g")
)

The tidyverse suite (specifically dplyr and tidyr) makes this process super intuitive. Here's how to do it:

  1. Add an index column for each Att entry within its ID group. This lets us generate the dynamic column names like Att_1, Att_2, etc.
  2. Pivot the data to wide format, filling any missing spots with NA.
library(tidyverse)

# Step 1: Add group-specific indices to name our future columns
df_with_indices <- df %>%
  group_by(ID) %>%
  mutate(att_col = paste0("Att_", row_number())) %>%
  ungroup()

# Step 2: Convert from long to wide format
wide_df <- df_with_indices %>%
  pivot_wider(
    id_cols = ID,
    names_from = att_col,
    values_from = Att,
    values_fill = NA  # Fill empty cells with NA
  )

# Check the result
print(wide_df)

When you run this, you'll get exactly the output you want:

# A tibble: 4 × 4
     ID Att_1 Att_2 Att_3
  <dbl> <chr> <chr> <chr>
1     1 a     b     c    
2     2 d     NA    NA   
3     3 e     f     NA   
4     4 g     NA    NA   

Method 2: Base R (No Extra Packages Needed)

If you don't want to install additional packages, you can use base R's built-in reshape() function instead:

# Add an index column for each ID group
df$att_index <- with(df, ave(seq_along(Att), ID, FUN = seq_along))

# Reshape to wide format
wide_df_base <- reshape(
  df,
  idvar = "ID",
  timevar = "att_index",
  direction = "wide",
  sep = "_"
)

# View the result
print(wide_df_base)

This will produce the same structure, with missing values automatically filled as NA.

Quick Extra Tip

If you specifically need N/A as a string instead of R's default NA value, you can tweak the tidyverse method to replace missing values:

wide_df_with_na_str <- wide_df %>%
  mutate(across(starts_with("Att_"), ~replace_na(., "N/A")))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:25:59