[英]Labeling Factors from another column of factors in R
I have a dtaframe with a column/vector of 2 factor levels...these can change, but there will always be 2 of them. 我有一个带有2个因子水平的列/向量的dtaframe ...这些可以改变,但是总会有2个。 I want to create a new column/vector that assigns a '0' for the first factor and a '1' for the second.....so no matter what my original factor field is i always get a column of zeros and ones.
我想创建一个新的列/向量,为第一个因子分配一个“ 0”,为第二个因子分配一个“ 1” .....所以无论我最初的因子字段是什么,我总会得到一列零和一。
PART col1 newcol
A a 0
A a 0
A a 0
A a 0
A b 1
A b 1
A b 1
A b 1
B c 0
B c 0
B c 0
B c 0
B d 1
B d 1
B d 1
B d 1
But col1 one could be b or c, or d or e...etc....either way I just always want to create the col with the '0' & '1'. 但是col1可能是b或c或d或e ...等...无论哪种方式,我一直都只想用'0'和'1'创建col。 It should be grouped by the first 'PART' column.
它应该按第一列“ PART”列进行分组。
Hope this makes sense... Paul. 希望这有道理...保罗。
You can try 你可以试试
library(dplyr)
df1 %>%
group_by(PART) %>%
mutate(newcol=factor(col1, labels=0:1))
#or
#mutate(newcol= as.numeric(factor(col1))-1)
# PART col1 newcol
#1 A a 0
#2 A a 0
#3 A a 0
#4 A a 0
#5 A b 1
#6 A b 1
#7 A b 1
#8 A b 1
#9 B c 0
#10 B c 0
#11 B c 0
#12 B c 0
#13 B d 1
#14 B d 1
#15 B d 1
#16 B d 1
Or using ave
from base R
或使用
base R
ave
with(df1, as.numeric(ave(as.character(col1), PART, FUN=factor)))-1
#[1] 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.