📌  相关文章
📜  如何在 R DataFrame 中将列拆分为多列?

📅  最后修改于: 2022-05-13 01:54:58.812000             🧑  作者: Mango

如何在 R DataFrame 中将列拆分为多列?

在本文中,我们将讨论如何在 R 编程语言中将数据帧中的一列拆分为多列。

方法一:使用stringr包库的str_split_fixed()函数

为了在 R 语言中将一列拆分为多列,我们使用 stringr 包库的 str_split_fixed()函数。 str_split_fixed()函数将字符串拆分为固定数量的片段。该函数接受字符串,分隔字符串的术语和它必须分成的部分作为参数,并返回拆分后的字符串。

示例:将列拆分为多列

R
# create sample data frame
df <- data.frame(Name=c('Priyank Mishra', 'Abhiraj Srivastava',
                        'Pawananjani Kumar'),
                 State= c("Uttar Pradesh", "Maharashtra", "Bihar"))
 
print(" Data frame before splitting: ")
df
 
# load stringr library
library(stringr)
 
# Split name column into firstname and last name
df[c('First Name', 'Last Name')] <- str_split_fixed(df$Name, ' ', 2)
 
# Rearrange columns and remove original name column
df <- df[c('First Name', 'Last Name', 'State')]
 
print(" Data frame after splitting: ")
df


R
# create sample data frame
df <- data.frame(Name=c('Priyank Mishra', 'Abhiraj Srivastava',
                        'Pawananjani Kumar'),
                 State= c("Uttar Pradesh", "Maharashtra", "Bihar"))
 
print(" Data frame before splitting: ")
df
 
# load dplyr and tidyr library
library(dplyr)
library(tidyr)
 
# Split name column into firstname and last name
df <- df %>% separate(Name, c('First Name', 'Last Name'))
 
print(" Data frame after splitting: ")
df


输出:

Data frame before splitting: 
               Name         State
1     Priyank Mishra Uttar Pradesh
2 Abhiraj Srivastava   Maharashtra
3  Pawananjani Kumar         Bihar
 Data frame after splitting: 
  First Name  Last Name         State
1     Priyank     Mishra Uttar Pradesh
2     Abhiraj Srivastava   Maharashtra
3 Pawananjani      Kumar         Bihar

方法二:使用dplyr包库的separate()函数

为了在 R 语言中将一列拆分为多列,我们使用 dplyr 包库的 separator()函数。 separate()函数使用正则表达式或数字位置将一个字符列分隔为多个列。该函数将输入字符向量作为参数,将向量中的输出列名作为参数,并返回最终数据向量。

示例:将列拆分为多列

R

# create sample data frame
df <- data.frame(Name=c('Priyank Mishra', 'Abhiraj Srivastava',
                        'Pawananjani Kumar'),
                 State= c("Uttar Pradesh", "Maharashtra", "Bihar"))
 
print(" Data frame before splitting: ")
df
 
# load dplyr and tidyr library
library(dplyr)
library(tidyr)
 
# Split name column into firstname and last name
df <- df %>% separate(Name, c('First Name', 'Last Name'))
 
print(" Data frame after splitting: ")
df

输出:

Data frame before splitting:
              Name         State
1     Priyank Mishra Uttar Pradesh
2 Abhiraj Srivastava   Maharashtra
3  Pawananjani Kumar         Bihar
Data frame after splitting:
 First Name  Last Name         State
1     Priyank     Mishra Uttar Pradesh
2     Abhiraj Srivastava   Maharashtra
3 Pawananjani      Kumar         Bihar