修改R因子?
假设在R中有一个Data.Frame对象,其中所有字符列已被转换为因子。 然后,我需要“修改”与数据帧中某一行关联的值 - 但将其保留为一个因子编码。 我首先需要提取一行,所以这就是我正在做的事情。 这是一个可重现的例子
a = c("ab", "ba", "ca")
b = c("ab", "dd", "da")
c = c("cd", "fa", "op")
data = data.frame(a,b,c, row.names = c("row1", "row2", "row3")
colnames(data) <- c("col1", "col2", "col3")
data[,"col1"] <- as.factor(data[,"col1"])
newdat <- data["row1",]
newdat["col1"] <- "ca"
当我将“ca”分配给newdat [“col1”]时,与数据中该列关联的Factor对象被字符串“ca”覆盖。 这不是预期的行为。 相反,我想修改编码newdat中存在哪个级别的数字值。 所以我想按如下方式更改newdat [“col1”]的内容:
之前:
Factor object, levels = c("ab", "ba", "ca"): 1 (the value it had)
后:
Factor object, levels = c("ab", "ba", "ca"): 3 (the value associated with the level "ca")
我怎样才能做到这一点?
你在做什么等同于:
x = factor(letters[1:4]) #factor
x1 = x[1] #factor; subset of 'x'
x1 = "c" #assign new value
即分配一个新的对象到现有的符号。 在你的例子中,你只需用“ca”替换newdat["col1"]
的“因子”。 相反,要分配一个因子(在NA
分配一个非等级结果),可以使用
x = factor(letters[1:4])
x1 = x[1]
x1[1] = "c" #factor; subset of 'x' with the 3rd level
在你的例子中(我使用local
来避免在下面再次改变newdat
):
str(newdat)
#'data.frame': 1 obs. of 3 variables:
# $ col1: Factor w/ 3 levels "ab","ba","ca": 1
# $ col2: Factor w/ 3 levels "ab","da","dd": 1
# $ col3: Factor w/ 3 levels "cd","fa","op": 1
local({ newdat["col1"] = "ca"; str(newdat) })
#'data.frame': 1 obs. of 3 variables:
# $ col1: chr "ca"
# $ col2: Factor w/ 3 levels "ab","da","dd": 1
# $ col3: Factor w/ 3 levels "cd","fa","op": 1
local({ newdat[1, "col1"] = "ca"; str(newdat) })
#'data.frame': 1 obs. of 3 variables:
# $ col1: Factor w/ 3 levels "ab","ba","ca": 3
# $ col2: Factor w/ 3 levels "ab","da","dd": 1
# $ col3: Factor w/ 3 levels "cd","fa","op": 1
local({ newdat[["col1"]][1] = "ca"; str(newdat) })
#'data.frame': 1 obs. of 3 variables:
# $ col1: Factor w/ 3 levels "ab","ba","ca": 3
# $ col2: Factor w/ 3 levels "ab","da","dd": 1
# $ col3: Factor w/ 3 levels "cd","fa","op": 1
链接地址: http://www.djcxy.com/p/29933.html
下一篇: Xcode 7.1: Property with retain or strong attribute must be of object type