如何将数据框列从因子转换为 R 中的字符?
在本文中,我们将讨论如何在 R 编程语言中将数据帧列从因子转换为字符。数据框可以将不同类型的列堆叠在一起以形成管状结构。可以通过数据框轻松修改列数据以及数据类型之间的转换。 R语言为我们提供了多种方法来模拟数据框列的数据类型转换:
方法一:使用transform()方法
transform() 方法可用于模拟在此方法的参数列表中指定的数据对象中的修改。必须将更改显式保存到相同的数据帧或新的数据帧中。此方法可用于向数据添加新变量或修改现有变量。
Syntax: transform(data, value)
Arguments :
- data : The data object to be modified
- value : The value to be added
最初,由 sapply() 方法返回的数据帧的 col3 的类是一个因子,它会在应用 transform() 方法时更改为字符。在此转换过程中保留数据。
例子:
R
# declare a dataframe
# different data type have been
# indicated for different cols
data_frame <- data.frame(
col1 = as.character(6:9),
col2 = factor(4:7),
col3 = factor(letters[1:4])
)
print("Original DataFrame")
print (data_frame)
# indicating the data type of each
# variable
sapply(data_frame, class)
# converting factor type column to
# character
data_frame_col3 <- transform(
data_frame, col3 = as.character(col3))
print("Modified col5 DataFrame")
print (data_frame_col3)
# indicating the data type of each variable
sapply(data_frame_col3, class)
R
# declare a dataframe
# different data type have been
# indicated for different cols
library(dplyr)
data_frame <- data.frame(
"col1" = as.character(6:9),
"col2" = factor(4:7),
"col3" = factor(letters[1:4])
)
print("Original DataFrame")
print (data_frame)
# indicating the data type of
# each variable
sapply(data_frame, class)
# converting factor type column
# to character
data_frame <- data_frame%>%mutate_at(
"col2", as.character)
print("Modified col2 DataFrame")
print (data_frame)
# indicating the data type of
# each variable
sapply(data_frame, class)
R
# declare a dataframe
# different data type have been
# indicated for different cols
data_frame <- data.frame(
col1 = as.character(6:9),
col2 = factor(c('tzx','hi','gfg','cse')),
col3 = factor(letters[1:4])
)
print("Original DataFrame")
print (data_frame)
# indicating the data type of each
# variable
sapply(data_frame, class)
# converting factor type column to
# character
data_frame[] <-lapply(data_frame, as.character)
print("Modified col5 DataFrame")
print (data_frame)
# indicating the data type of each variable
sapply(data_frame, class)
输出
[1] "Original DataFrame"
col1 col2 col3
1 6 4 a
2 7 5 b
3 8 6 c
4 9 7 d
col1 col2 col3
"factor" "factor" "factor"
[1] "Modified col5 DataFrame"
col1 col2 col3
1 6 4 a
2 7 5 b
3 8 6 c
4 9 7 d
col1 col2 col3
"factor" "factor" "character"
方法二:使用dplyr包
dplyr 包用于执行数据操作和抽象。它是提供大量内置功能的tidyverse包的子代。它可用于执行数据突变,使用 mutate_at() 方法,其中附加变量作为现有变量的函数添加。
Syntax: mutate_at(.data, .vars, .funs)
Arguments :
- .data : The data to modify
- .var : The variable to modify
- .funs : The function to apply over the variable to be modified.
最初, sapply() 方法返回的数据帧的 col2 的类是一个因子,它会在应用 mutate_at() 方法时更改为字符。在此转换过程中保留数据。
例子:
电阻
# declare a dataframe
# different data type have been
# indicated for different cols
library(dplyr)
data_frame <- data.frame(
"col1" = as.character(6:9),
"col2" = factor(4:7),
"col3" = factor(letters[1:4])
)
print("Original DataFrame")
print (data_frame)
# indicating the data type of
# each variable
sapply(data_frame, class)
# converting factor type column
# to character
data_frame <- data_frame%>%mutate_at(
"col2", as.character)
print("Modified col2 DataFrame")
print (data_frame)
# indicating the data type of
# each variable
sapply(data_frame, class)
输出
[1] "Original DataFrame"
> print (data_frame)
col1 col2 col3
1 6 4 a
2 7 5 b
3 8 6 c
4 9 7 d
col1 col2 col3
"factor" "factor" "factor"
[1] "Modified col2 DataFrame"
col1 col2 col3
1 6 4 a
2 7 5 b
3 8 6 c
4 9 7 d
col1 col2 col3
"factor" "character" "factor"
方法 3:使用lapply()方法
R 中的 lapply 方法用于对列表对象进行操作,并返回与输入列表对象长度相同的列表对象。此输出列表的每个元素都是将 FUN 应用于列表的每个元素的结果。
Syntax: lapply(X, FUN, …)
Arguments :
- df: The dataframe object to make modifications to.
- FUN: The function to be applied to each element of the dataframe.
lapply() 方法返回的是 R 中的向量列表对象。 然而,我们将这个结果的输出保存到 dataframe 变量的列表对象中,即 data_frame[],它将列表隐式转换为数据帧,无需显式转换。它将因子列的转换应用于整个数据帧中的字符。因此,所有列类型都更改为字符。
例子:
电阻
# declare a dataframe
# different data type have been
# indicated for different cols
data_frame <- data.frame(
col1 = as.character(6:9),
col2 = factor(c('tzx','hi','gfg','cse')),
col3 = factor(letters[1:4])
)
print("Original DataFrame")
print (data_frame)
# indicating the data type of each
# variable
sapply(data_frame, class)
# converting factor type column to
# character
data_frame[] <-lapply(data_frame, as.character)
print("Modified col5 DataFrame")
print (data_frame)
# indicating the data type of each variable
sapply(data_frame, class)
输出
[1] "Original DataFrame"
col1 col2 col3
1 6 tzx a
2 7 hi b
3 8 gfg c
4 9 cse d
col1 col2 col3
"factor" "factor" "factor"
[1] "Modified col5 DataFrame"
col1 col2 col3
1 6 tzx a
2 7 hi b
3 8 gfg c
4 9 cse d
col1 col2 col3
"character" "character" "character"