如何在R中将因子转换为字符?
在本文中,我们将讨论如何将因子类转换为 R 编程语言中的字符类。
方法一:将单因子向量转换为字符向量
要将单个因子向量转换为字符向量,我们使用 as。 R 语言的字符()函数,并将所需的因子向量作为参数传递。
句法:
character_vector <- as.character( Factor_vector )
例子:
在此示例中,我们使用 as 将因子向量转换为字符向量。字符()函数。
R
# create factor vector
Input <- factor(c("Geeks", "for", "Geek"))
# print input factor vector and its class
print("Input vector:")
Input
print("Input Data Class:")
class(Input)
# Convert factor vector to character vector
Output <- as.character(Input)
# print Output character vector and its class
print("Output vector:")
Output
print("Output Data Class:")
class(Output)
R
# create factor vector
x <- factor(c("Geeks", "for", "Geek"))
y <- c(1,2,3)
Input <- data.frame( x, y )
# print input factor vector and its data Class
print("Input vector:")
Input
print("Input Data Class:")
class(Input$x)
# Convert factor vector to character vector
Output <- Input
Output$x <- as.character(Input$x)
# print Output character vector and its Class
print("Output vector:")
Output
print("Output Data Class:")
class(Output$x)
R
# create Input dataframe
x <- factor(c("Geeks", "for", "Geek"))
y <- c(1,2,3)
z <- factor(c("Complete", "Interview", "Preparation"))
Input <- data.frame( x, y, z )
# print input data frame and its data Class
print("Input dataframe:")
Input
print("Input Data Class:")
sapply(Input, class)
#Convert factor vector to character vector
Output <- Input
factor_columns <- sapply(Output, is.factor)
Output[factor_columns] <- lapply(Output[factor_columns], as.character)
# print Output character vector and its Class
print("Output dataframe:")
Output
print("Output Data Class:")
sapply(Output, class)
R
# create Input dataframe
x <- c("Geeks", "for", "Geek")
y <- c(1,2,3)
z <- factor(c("Complete", "Interview", "Preparation"))
df <- data.frame( x, y, z )
# print input data frame and its data Class
print("Input dataframe:")
df
print("Input Data Class:")
sapply(df, class)
#Convert all columns of data frame to character vector
df <- lapply(df, as.character)
# print Output character vector and its Class
print("Output dataframe:")
df
print("Output Data Class:")
sapply(df, class)
输出:
Input vector:
Geeks for Geek
Input Data Class:
factor
Output vector:
"Geeks" "for" "Geek"
Output Data Class:
character
方法 2:将因子向量列转换为数据框中的字符向量列
要将已知因子向量转换为字符向量,我们创建输入数据帧的副本。然后使用 as。 R 语言的字符()函数,并将数据帧的所需因子向量列作为参数传递。
句法:
Output$factor_vector_column <- as.character( Output$factor_vector_column )
例子:
在此示例中,我们使用 as 将因子向量列转换为字符向量列。字符()函数。
R
# create factor vector
x <- factor(c("Geeks", "for", "Geek"))
y <- c(1,2,3)
Input <- data.frame( x, y )
# print input factor vector and its data Class
print("Input vector:")
Input
print("Input Data Class:")
class(Input$x)
# Convert factor vector to character vector
Output <- Input
Output$x <- as.character(Input$x)
# print Output character vector and its Class
print("Output vector:")
Output
print("Output Data Class:")
class(Output$x)
输出:
Input vector:
x y
1 Geeks 1
2 for 2
3 Geek 3
Input Data Class:
factor
Output vector:
x y
1 Geeks 1
2 for 2
3 Geek 3
Output Data Class:
character
方法 3:将因子向量列转换为列未知的数据框中的字符向量列
要将已知因子向量转换为字符向量,我们创建输入数据帧的副本。然后使用 is.factor() 条件语句查找具有数据类因子的列。然后使用 as。 R 语言的字符()函数,并将数据帧的已识别因子向量列作为参数传递。
句法:
factor_columns <- sapply(Output, is.factor)
Output[factor_columns] <- lapply(Output[factor_columns], as.character)
例子:
在此示例中,我们使用 as 将所有因子向量列转换为字符向量列。由 is.factor() 条件语句标识的字符()函数。
R
# create Input dataframe
x <- factor(c("Geeks", "for", "Geek"))
y <- c(1,2,3)
z <- factor(c("Complete", "Interview", "Preparation"))
Input <- data.frame( x, y, z )
# print input data frame and its data Class
print("Input dataframe:")
Input
print("Input Data Class:")
sapply(Input, class)
#Convert factor vector to character vector
Output <- Input
factor_columns <- sapply(Output, is.factor)
Output[factor_columns] <- lapply(Output[factor_columns], as.character)
# print Output character vector and its Class
print("Output dataframe:")
Output
print("Output Data Class:")
sapply(Output, class)
输出:
Input dataframe:
x y z
1 Geeks 1 Complete
2 for 2 Interview
3 Geek 3 Preparation
Input Data Class:
x y z
"factor" "numeric" "factor"
Output dataframe:
x y z
1 Geeks 1 Complete
2 for 2 Interview
3 Geek 3 Preparation
Output Data Class:
x y z
"character" "numeric" "character"
方法4:将Data frame的所有列转换为字符
要将数据框的所有列转换为字符,我们使用 as.apply()函数。字符参数。 lapply()函数将给定函数应用于提供的数据帧。
句法:
dataframe <- lapply(dataframe, as.character)
例子:
在此示例中,我们使用 as 将数据帧的所有列转换为字符向量列。使用 lapply()函数的字符函数。
R
# create Input dataframe
x <- c("Geeks", "for", "Geek")
y <- c(1,2,3)
z <- factor(c("Complete", "Interview", "Preparation"))
df <- data.frame( x, y, z )
# print input data frame and its data Class
print("Input dataframe:")
df
print("Input Data Class:")
sapply(df, class)
#Convert all columns of data frame to character vector
df <- lapply(df, as.character)
# print Output character vector and its Class
print("Output dataframe:")
df
print("Output Data Class:")
sapply(df, class)
输出:
Input dataframe:
x y z
1 Geeks 1 Complete
2 for 2 Interview
3 Geek 3 Preparation
Input Data Class:
x y z
"character" "numeric" "factor"
Output dataframe:
$x
[1] "Geeks" "for" "Geek"
$y
[1] "1" "2" "3"
$z
[1] "Complete" "Interview" "Preparation"
Output Data Class:
x y z
"character" "character" "character"