考虑这个数据框架。
data <- structure(list(Sample1 = structure(1:10, .Label = c("100", "101",
"102", "103", "104", "105", "106", "107", "108", "109"), class = "factor"),
Sample2 = structure(1:10, .Label = c("1", "10", "100", "101",
"102", "103", "104", "105", "106", "107"), class = "factor"),
Bray = c(0, -0.093229941171876, -0.101979485248057, -0.109527276554936,
-0.107218514918197, -0.12034240232431, -0.0867499433287722,
-0.0805681841664597, -0.086656413429741, -0.0871426867635103
), Space = c(0, 6.6986864383997, 6.6053482118659, 6.01295268566118,
6.43471833105382, 7.43673483458971, 7.78171093012327, 8.97899771689469,
9.32053646524705, 10.2821447179078), Time = c(0, 0, 42, 42,
42, 42, 42, 42, 42, 42)), .Names = c("Sample1", "Sample2",
"Bray", "Space", "Time"), row.names = c(NA, 10L), class = "data.frame")
我想介绍一个带有因子的新专栏&#34; Color&#34;水平&#34;是&#34;和&#34;不&#34;取决于Sample1
或Sample2
中是否显示某些值。
在这种情况下,列Sample1
或Sample2
中任何值介于100和104之间的行都应该得到&#34;是&#34;。
怎么做?
答案 0 :(得分:2)
我们将“示例”列转换为numeric
,然后使用</>
获取逻辑向量,转换为数字索引并将其替换为No/Yes
data[1:2] <- lapply(data[1:2], function(x) as.numeric(as.character(x)))
data$Color <- with(data, factor(c("No", "Yes")[((Sample1 < 104 & Sample1 > 100) |
(Sample2 < 104 & Sample2 > 100))+1]))
注意:如果条件包括 100和104,请将</>
更改为<=/>=
或者@Frank提到%in%
也适用于factor
列(不将“示例”列更改为numeric
)
data$Color <- with(data, factor(c("No", "Yes")[((Sample1 %in% 100:104)|
(Sample2 %in% 100:104)) + 1]))