如何在 R 数据框列中移除字符?
要在 R 数据框 列中移除字符,我们可以使用 gsub() 函数,它将用空格替换字符。例如,如果我们有一个名为 df 的数据框,其中包含一个字符列,比如 x,其中每个值中都有一个字符 ID,那么可以使用命令 gsub("ID","",as.character(df$x)) 来移除它。
示例 1
考虑下面的数据框 −
> x1<-sample(c("Male1","Female1"),20,replace=TRUE) > x2<-rpois(20,5) > df1<-data.frame(x1,x2) > df1
输出
x1 x2 1 Male1 8 2 Female1 4 3 Male1 9 4 Male1 2 5 Male1 7 6 Female1 5 7 Male1 3 8 Female1 6 9 Female1 4 10 Female1 3 11 Male1 5 12 Male1 4 13 Female1 7 14 Male1 6 15 Female1 5 16 Male1 3 17 Female1 10 18 Female1 4 19 Male1 7 20 Female1 3
从 x1 中所有值移除 1 −
> df1$x1<-gsub("1","",as.character(df1$x1)) > df1
输出
x1 x2 1 Male 8 2 Female 4 3 Male 9 4 Male 2 5 Male 7 6 Female 5 7 Male 3 8 Female 6 9 Female 4 10 Female 3 11 Male 5 12 Male 4 13 Female 7 14 Male 6 15 Female 5 16 Male 3 17 Female 10 18 Female 4 19 Male 7 20 Female 3
示例 2
> y1<-sample(c("Hot#","Cold#"),20,replace=TRUE) > y2<-rnorm(20) > df2<-data.frame(y1,y2) > df2
输出
y1 y2 1 Cold# 0.1725073 2 Hot# -0.4692023 3 Hot# 2.4386160 4 Cold# -1.3592685 5 Hot# 0.4500866 6 Cold# -0.7023989 7 Cold# 0.7383042 8 Hot# -1.7325754 9 Hot# 0.2053521 10 Hot# -2.0123750 11 Cold# -0.7203218 12 Hot# 1.2389079 13 Cold# -0.6401144 14 Cold# -0.6136728 15 Hot# 1.2877643 16 Cold# 0.4291966 17 Cold# 1.8316996 18 Hot# 0.8165865 19 Cold# -0.3978465 20 Cold# -0.9352063
从 y1 中所有值移除 # −
> df2$y1<-gsub("#","",as.character(df2$y1)) > df2
输出
y1 y2 1 Cold 0.1725073 2 Hot -0.4692023 3 Hot 2.4386160 4 Cold -1.3592685 5 Hot 0.4500866 6 Cold -0.7023989 7 Cold 0.7383042 8 Hot -1.7325754 9 Hot 0.2053521 10 Hot -2.0123750 11 Cold -0.7203218 12 Hot 1.2389079 13 Cold -0.6401144 14 Cold -0.6136728 15 Hot 1.2877643 16 Cold 0.4291966 17 Cold 1.8316996 18 Hot 0.8165865 19 Cold -0.3978465 20 Cold -0.9352063
广告