如果声明,则执行功能,说声明

时间:2018-07-24 16:13:49

标签: r

我正在尝试编写一个简单的if语句。我有这样的数据

from kivy.app import App 
from kivy.uix.screenmanager import ScreenManager, Screen, 
SlideTransition 
from kivy.properties import ObjectProperty
from kivy.uix.image import Image
from kivy.graphics import Color 
from kivy.graphics import Rectangle 
from kivy.core.window import Window 

import os

from connected import Connected


class Login(Screen):
    pass

class Connected(Screen):
    pass


class Patient(Screen):
    pass 


class Manager(ScreenManager):

    patient = ObjectProperty(None)
    connected = ObjectProperty(None)
    login = ObjectProperty(None)

class ScreensApp(App):

    def build(self):

        m = Manager(transition=SlideTransition())   
        return m



if __name__ == '__main__':
    ScreensApp().run()

基本上试图做到这一点:

Number  Description        Type
1       Snow in road       "weather"
2       Ice on Roof        "Weather"
3       Dog bite           "Not Classified"

期望的结果将是我在代码中先前声明的函数在第3行if(data$type == "Not Classified") {sapply(data$description, colcheck)} else "Not Classified" 行上运行。由于某些原因,我不断收到相同的错误:

"Not Classified"

"the condition has length > 1 and only the first element will be used". 是先前创建的功能。我尝试过colcheck,最后取出IfElse,并在函数前面添加了else,但是它们不起作用。我试图过滤数据以仅在do所在的行上使用该函数。谢谢

2 个答案:

答案 0 :(得分:0)

问题在于data$type是一个长度大于1的向量。==运算符仅比较单个值,当您将其传递给向量时,它仅采用第一个值,而不是失败

您要执行的操作是使用applydplyr::mutate将测试应用于data$type的每个元素:

data <- data.frame('Number' = c(1,2,3),
                   'Description' = c('Snow in road', 'Ice on Roof', 'Dog bite'),
                   'Type' = c('weather', 'Weather', 'Not Classified'))

  Number  Description           Type
1      1 Snow in road        weather
2      2  Ice on Roof        Weather
3      3     Dog bite Not Classified

colcheck的示例功能:

colcheck <- function(x) return(paste0('x',x,'x'))

使用基础apply

apply(data, 1, function(row) {
    if (row['Type'] == 'Not Classified') {
        return(colcheck(row['Description']))
    } else {
        return(row['Description'])
    }
})

[1] "Snow in road" "Ice on Roof"  "xDog bitex"  

或使用dplyr

library(dplyr)
data %>%
    mutate('colcheck' = if_else(Type == 'Not Classified',
                                colcheck(Description),
                                paste(Description)))

  Number  Description           Type     colcheck
1      1 Snow in road        weather Snow in road
2      2  Ice on Roof        Weather  Ice on Roof
3      3     Dog bite Not Classified   xDog bitex

答案 1 :(得分:0)

我没有使用函数,而是为每个分类做了一个大的ifelse语句。例如,

ifelse(data$type = grepl('Snow| Ice | Rain',data$description, ignore.case = TRUE)),"Weather","Not Classified")

另一个是Nonweather,其结果将创建一个新列。

ifelse(data$type1 = grepl('Dog Bite| Stinky House| Mice Infestation',data$description, ignore.case = TRUE)),'Non-Weather',"Not Classified")

然后,由于非天气分类取决于天气分类,因此我简化了该if语句,以接受任何天气结果,然后继续使用非天气词,所有这些都在与天气相邻的新列中其他。

data$Type2<-ifelse(data$Type == "Weather", 
"Weather",data$Type1)

然后我删除了“ Type1”和“ Type”列,并将“ type 2”的名称更改为“ Type”,以保留所需的结果。

它可能很长,也很草率,但确实成功了!