我做了一个新功能:
newFunction <- function (varX) {
lst_a <- c("aa", "bb", "cc")
lst_b <- c("dd", "ee", "ff")
lst_c <- c("gg", "hh", "ii")
ifelse(varX %in% lst_a , x.out <- "List A" , x.out <- "Other List" )
ifelse(varX %in% lst_b , x.out <- "List B" , x.out )
ifelse(varX %in% lst_c , x.out <- "List C" , x.out)
return(x.out)
}
当我测试该功能时,它运行良好。但是,当我将其应用于数据框时,它将不起作用:
df <- as.data.frame(c("aa","cc","kk", "nn"))
df$new <- newFunction((df[,1]))
我希望获得“列表A”,“列表A”,“其他列表”,“其他列表”-但是我没有这种运气,而我留下的是“其他列表”,“其他列表”,“其他列表”,“其他列表”
答案 0 :(得分:1)
您可以尝试
<div class="d-flex">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" v-model="ClienteBusca.value" placeholder="Buscar cliente">
<span class="input-group-btn">
<button type="button" v-on:click.prevent="buscarCliente" class="btn btn-primary">Buscar</button>
</span>
</div>
</div>
<div class="form-group">
<select class="form-control" id="selectedCliente">
<option v-for="cliente in Clientes" v-bind:value="cliente">test test test test test test test test test test test test test test test </option>
</select>
</div>
</div>
或
sapply (df[,1], newFunction)
答案 1 :(得分:1)
这样做:
newFunction <- function (varX) {
lst_a <- c("aa", "bb", "cc")
lst_b <- c("dd", "ee", "ff")
lst_c <- c("gg", "hh", "ii")
x.out = ifelse(varX %in% lst_a , "List A" ,
ifelse(varX %in% lst_b , "List B",
ifelse(varX %in% lst_c , "List C" , "Other List")))
return(x.out)
}
现在,它已被矢量化,您可以执行df$new <- newFunction(df[,1])
,它的效率要高得多。
我强烈建议reading this R-FAQ来了解ifelse
与if(){}else{}
。