重新编码/重新调整不同级别的 data.frame 因子

发布于 2024-08-22 16:54:52 字数 732 浏览 3 评论 0原文

每次当我必须重新编码一组变量时,我都会想到 SPSS 重新编码功能。我必须承认这非常简单。 car 包中有一个类似的 recode 函数,它也能达到目的,但我们假设我想用 factor 来完成任务。

我有 data.frame ,其中包含多个值范围从 1 到 7 的变量。我想“反转”变量值,因此将 1s 替换为 7s,将 2s 替换为 6s,将 3s 替换为 5s 等。我可以利用factor 函数:

# create dummy factor
set.seed(100)
x <- as.factor(round(runif(100,1,7)))
y <- factor(x, levels = rev(levels(x)))

如果我运行:

> levels(x)
[1] "1" "2" "3" "4" "5" "6" "7"
> levels(y)
[1] "7" "6" "5" "4" "3" "2" "1"

当我想重新编码不具有​​相同级别的因子时,问题就开始了。如果某个因子 z 具有级别 c("1", "3", "4", "6", "7"),我是否有机会“反转”级别,以便利用factor函数计算1=7、2=6、3=5等?

其他有效的重新编码功能应该足够了!

Each time when I have to recode some set of variables, I have SPSS recode function in mind. I must admit that it's quite straightforward. There's a similar recode function in car package, and it does the trick, but let's presuppose that I want to get things done with factor.

I have data.frame with several variables with value range from 1 to 7. I want to "reverse" variable values, hence replacing 1s with 7s, 2s with 6s, 3s with 5s etc. I can utilize factor function:

# create dummy factor
set.seed(100)
x <- as.factor(round(runif(100,1,7)))
y <- factor(x, levels = rev(levels(x)))

And if I run:

> levels(x)
[1] "1" "2" "3" "4" "5" "6" "7"
> levels(y)
[1] "7" "6" "5" "4" "3" "2" "1"

Problem starts when I want to recode factors that do not have equal levels. If some factor, z, has levels c("1", "3", "4", "6", "7"), is there any chance that I can "reverse" levels so 1=7, 2=6, 3=5 etc. by utilizing factor function?

Other efficient recode functions should suffice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残花月 2024-08-29 16:54:52

您必须为因子提供 levels 参数(如 Dirk 所写):

set.seed(2342472)
( x <- round(runif(10,1,7)) )
#  [1] 7 5 5 3 1 2 5 3 3 2
( xf <- as.factor(x) )
# [1] 7 5 5 3 1 2 5 3 3 2
# Levels: 1 2 3 5 7
( yf <- factor(x,levels=7:1) )
# [1] 7 5 5 3 1 2 5 3 3 2
# Levels: 7 6 5 4 3 2 1

您也可以对现有因子执行此操作,

( yxf <- factor(xf,levels=7:1) )
# [1] 7 5 5 3 1 2 5 3 3 2
#Levels: 7 6 5 4 3 2 1

正如您所看到的级别已按所需顺序扩展。

You must provide levels argument to factor (as Dirk wrote):

set.seed(2342472)
( x <- round(runif(10,1,7)) )
#  [1] 7 5 5 3 1 2 5 3 3 2
( xf <- as.factor(x) )
# [1] 7 5 5 3 1 2 5 3 3 2
# Levels: 1 2 3 5 7
( yf <- factor(x,levels=7:1) )
# [1] 7 5 5 3 1 2 5 3 3 2
# Levels: 7 6 5 4 3 2 1

you could do this on existing factor too

( yxf <- factor(xf,levels=7:1) )
# [1] 7 5 5 3 1 2 5 3 3 2
#Levels: 7 6 5 4 3 2 1

As you see levels were extended in desire order.

維他命╮ 2024-08-29 16:54:52

是的,只需分配到级别

R> set.seed(100)
R> x <- as.factor(round(runif(100,1,7)))
R> table(x)
x
 1  2  3  4  5  6  7 
 3 16 20 19 18 17  7 
R> levels(x) <- LETTERS[1:7]
R> table(x)
x
 A  B  C  D  E  F  G 
 3 16 20 19 18 17  7 
R> 

Yes, just assign to levels:

R> set.seed(100)
R> x <- as.factor(round(runif(100,1,7)))
R> table(x)
x
 1  2  3  4  5  6  7 
 3 16 20 19 18 17  7 
R> levels(x) <- LETTERS[1:7]
R> table(x)
x
 A  B  C  D  E  F  G 
 3 16 20 19 18 17  7 
R> 
一萌ing 2024-08-29 16:54:52

如果您完成了因子级别,那么您就可以开始:

df <- data.frame(x=factor(c(2,4,5,6)))
df$x <- factor(df$x, levels = 7:1)
table(df$x)

7 6 5 4 3 2 1 
0 1 1 1 0 1 0 

If you complete the factor levels you're good to go:

df <- data.frame(x=factor(c(2,4,5,6)))
df$x <- factor(df$x, levels = 7:1)
table(df$x)

7 6 5 4 3 2 1 
0 1 1 1 0 1 0 
撞了怀 2024-08-29 16:54:52

在这种情况下,既然你有数字,为什么不直接使用模运算来转换数字呢?

例如,

levels(x) <- as.character((6*as.numeric(levels(x)))%%7+1)

如果使用更大的范围,请适当修改 6 和 7。

In this case, since you have numbers, why not just transform the numbers using modular arithmetic?

eg

levels(x) <- as.character((6*as.numeric(levels(x)))%%7+1)

Modify the 6 and 7 as appropriate if using larger ranges.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文