这是我的工作代码:如何让R在if或ifelse语句中识别一对组合变量?
#create two random numbers and link them to letters
a1r <- sample(1:4, 1)
if (a1r==1) {
a1 <- "B"
} else {
if (a1r==2) {
a1 <- "b"
} else {
if (a1r==3) {
a1 <- "R"
} else {
if (a1r==4) {
a1 <- "r"
}}}}
a2r <- sample(1:4, 1)
if (a2r==1) {
a2 <- "B"
} else {
if (a2r==2) {
a2 <- "R"
} else {
if (a2r==3) {
a2 <- "R"
} else {
if (a2r==4) {
a2 <- "r"
}}}}
#combine the letters
acom <- c(a1,a2)
acom
[1] "R" "b"
现在的一部分,我不能去上班,我该如何使用变量“ACOM” if或ifelse语句?我相信我只是格式错误的地方。这是我能得到的最接近的。出于测试目的,我跳过了随机部分和刚刚创建的格式相同的变量:
testvar <- c("A","B")
ifelse(testvar=="AB", test1 <- 1, test1 <- 2)
test1
[1] 2
如何设置格式要么是原始数据(保持AB再分配结构),以便它可以很容易地作为一个变量或如何使用我可以格式化ifelse语句来读取当前变量吗?
谢谢!
谢谢!这让我走上了正确的道路。 :D – jdfinch3 2014-09-06 03:33:39