我一直在尝试用行名称替换列中的多个值。在我的实际数据中,它们代表了一个主观测试,在该特定点被记录为阳性或阴性。我必须在数据框中对其进行重新分类,而我所要做的就是样本ID(即行名)。
我想知道是否有一种方法可以一次处理多个值,而不是手动更改每个特定值。我看了this question。我尝试过,
dat <- structure(list(A = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("1", "0"), class = "factor"),B = structure(c(1L,1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("0", "1"), class = "factor"),C = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("nd","0", "1"), class = "factor"),D = structure(c(1L, 1L, 1L,2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L,2L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"),E = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0","1"), class = "factor")),.Names = c("A", "B", "C", "D", "E"), class = "data.frame", row.names = c(NA, 24L))
dat$result <- as.integer(rowSums(dat[,1:5] == "1")> 0)
dn <- c("1","5","7","10","14","15","16")
dat$result[dn] <- "3"
请有人帮我这个忙。
答案 0 :(得分:1)
dn
必须为数字,而不是字符。
以dn作为字符运行命令时,将仅获得NA值:
dat$result[dn]
# [1] NA NA NA NA NA NA NA
如果将dn
更改为数字,则会得到正确的值:
dat$result[as.numeric(dn)]
# [1] 0 1 0 1 0 1 0
然后您可以像这样分配新值:
dat$result[as.numeric(dn)] <- 3
dat$result
# [1] 3 0 1 1 3 1 3 1 0 3 0 1 1 3 3 3 1 1 1 0 0 1 0 1
这不会通过row.name来过滤data.frame,而是通过indees来过滤,但是由于它是一个有序的序列,因此您可以仅使用这些索引进行转换。还是需要基于row.names进行匹配?
要通过row.names
进行过滤,您可以执行以下操作:
## Filter by rownames
row.names(dat) <- paste0("row_", row.names(dat))
dat
dn <- c("row_1","row_5","row_7")
dat[row.names(dat) %in% dn,]$result <- 3
dat