RStudio stopped running code

Rstudio can't run the whole code as it stopped when it reaches the last function and do nothing, also I can't run any other code. I tried the memory functions to increase the memory but it did not solve the proplem.


adult<-read.csv('adult_sal.csv')

head(adult)

library(dplyr)

adult <- adult <- select(adult,-X)

head(adult)
summary(adult)

str(adult)
table(adult$type_employer)

unemp <- function(job){
  job <- as.character(job)
  if(job== 'Never-worked' | job=='Without-pay'){
    return('Unemployed')
  }else{
    return(job)
  }
}



adult$type_employer <- sapply(adult$type_employer,unemp)

group_emp <- function(job){
  if (job=='Local-gov' | job=='State-gov'){
    return('SL-gov')
  }else if (job=='Self-emp-inc' | job=='Self-emp-not-inc'){
    return('self-emp')
  }else{
    return(job)
  }
}

adult$type_employer <- sapply(adult$type_employer,group_emp)

table(adult$type_employer)

status.m <- function(married){
  if(married=='Married-spouse-absent' | married == 'Married-AF-spouse' | married== 'Married-civ-spouse'){
    return('Married')
  }else if (married=='Separated' | married=='Divorced' | married=='Widowed'){
    return('Not-Married')
  }else{
    return(married)
  }
}


adult$marital <- sapply(adult$marital,status.m)

table(adult$marital)


table(adult$country)

Asia <- c('China','Hong','India','Iran','Cambodia','Japan', 'Laos' ,
          'Philippines' ,'Vietnam' ,'Taiwan', 'Thailand')

North.America <- c('Canada','United-States','Puerto-Rico' )

Europe <- c('England' ,'France', 'Germany' ,'Greece','Holand-Netherlands','Hungary',
            'Ireland','Italy','Poland','Portugal','Scotland','Yugoslavia')

Latin.and.South.America <- c('Columbia','Cuba','Dominican-Republic','Ecuador',
                             'El-Salvador','Guatemala','Haiti','Honduras',
                             'Mexico','Nicaragua','Outlying-US(Guam-USVI-etc)','Peru',
                             'Jamaica','Trinadad&Tobago')
Other <- c('South')


group_country <- function(ctry){
  if (ctry %in% Asia){
    return('Asia')
  }else if (ctry %in% North.America){
    return('North.America')
  }else if (ctry %in% Europe){
    return('Europe')
  }else if (ctry %in% Latin.and.South.America){
    return('Latin.and.South.America')
  }else{
    return('Other')      
  }
  
  adult$country <- sapply(adult$country,group_country)
  
  table(adult$country)
  
  
  
  adult<-read.csv('adult_sal.csv')
  
  head(adult)
  
  library(dplyr)
  
  adult <- adult <- select(adult,-X)
  
  head(adult)
  summary(adult)
  
  str(adult)
  table(adult$type_employer)
  
  unemp <- function(job){
    job <- as.character(job)
    if(job== 'Never-worked' | job=='Without-pay'){
      return('Unemployed')
    }else{
      return(job)
    }
  }
  
  
  
  adult$type_employer <- sapply(adult$type_employer,unemp)
  
  group_emp <- function(job){
    if (job=='Local-gov' | job=='State-gov'){
      return('SL-gov')
    }else if (job=='Self-emp-inc' | job=='Self-emp-not-inc'){
      return('self-emp')
    }else{
      return(job)
    }
  }
  
  adult$type_employer <- sapply(adult$type_employer,group_emp)
  
  table(adult$type_employer)
  
  status.m <- function(married){
    if(married=='Married-spouse-absent' | married == 'Married-AF-spouse' | married== 'Married-civ-spouse'){
      return('Married')
    }else if (married=='Separated' | married=='Divorced' | married=='Widowed'){
      return('Not-Married')
    }else{
      return(married)
    }
  }
  
  
  adult$marital <- sapply(adult$marital,status.m)
  
  table(adult$marital)
  
  
  table(adult$country)
  
  Asia <- c('China','Hong','India','Iran','Cambodia','Japan', 'Laos' ,
            'Philippines' ,'Vietnam' ,'Taiwan', 'Thailand')
  
  North.America <- c('Canada','United-States','Puerto-Rico' )
  
  Europe <- c('England' ,'France', 'Germany' ,'Greece','Holand-Netherlands','Hungary',
              'Ireland','Italy','Poland','Portugal','Scotland','Yugoslavia')
  
  Latin.and.South.America <- c('Columbia','Cuba','Dominican-Republic','Ecuador',
                               'El-Salvador','Guatemala','Haiti','Honduras',
                               'Mexico','Nicaragua','Outlying-US(Guam-USVI-etc)','Peru',
                               'Jamaica','Trinadad&Tobago')
  Other <- c('South')
  
  
  group_country1 <- function(ctru){
    if (ctru %in% Asia){
      return('Asia')
    }else if (ctru %in% North.America){
      return('North.America')
    }else if (ctru %in% Europe){
      return('Europe')
    }else if (ctru %in% Latin.and.South.America){
      return('Latin.and.South.America')
    }else{
      return('Other')      
    }
    
    adult$country <- sapply(adult$country,group_country1)
    
    table(adult$country)

I think this is because you are missing the closing bracket in your last function definition.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

well, I can't run anything else .

> adult<-read.csv('adult_sal.csv')
> 
> head(adult)
  X age    type_employer fnlwgt education education_num            marital
1 1  39        State-gov  77516 Bachelors            13      Never-married
2 2  50 Self-emp-not-inc  83311 Bachelors            13 Married-civ-spouse
3 3  38          Private 215646   HS-grad             9           Divorced
4 4  53          Private 234721      11th             7 Married-civ-spouse
5 5  28          Private 338409 Bachelors            13 Married-civ-spouse
6 6  37          Private 284582   Masters            14 Married-civ-spouse
         occupation  relationship  race    sex capital_gain capital_loss
1      Adm-clerical Not-in-family White   Male         2174            0
2   Exec-managerial       Husband White   Male            0            0
3 Handlers-cleaners Not-in-family White   Male            0            0
4 Handlers-cleaners       Husband Black   Male            0            0
5    Prof-specialty          Wife Black Female            0            0
6   Exec-managerial          Wife White Female            0            0
  hr_per_week       country income
1          40 United-States  <=50K
2          13 United-States  <=50K
3          40 United-States  <=50K
4          40 United-States  <=50K
5          40          Cuba  <=50K
6          40 United-States  <=50K
> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> adult <- adult <- select(adult,-X)
> head(adult)
  age    type_employer fnlwgt education education_num            marital
1  39        State-gov  77516 Bachelors            13      Never-married
2  50 Self-emp-not-inc  83311 Bachelors            13 Married-civ-spouse
3  38          Private 215646   HS-grad             9           Divorced
4  53          Private 234721      11th             7 Married-civ-spouse
5  28          Private 338409 Bachelors            13 Married-civ-spouse
6  37          Private 284582   Masters            14 Married-civ-spouse
         occupation  relationship  race    sex capital_gain capital_loss
1      Adm-clerical Not-in-family White   Male         2174            0
2   Exec-managerial       Husband White   Male            0            0
3 Handlers-cleaners Not-in-family White   Male            0            0
4 Handlers-cleaners       Husband Black   Male            0            0
5    Prof-specialty          Wife Black Female            0            0
6   Exec-managerial          Wife White Female            0            0
  hr_per_week       country income
1          40 United-States  <=50K
2          13 United-States  <=50K
3          40 United-States  <=50K
4          40 United-States  <=50K
5          40          Cuba  <=50K
6          40 United-States  <=50K
> summary(adult)
      age        type_employer          fnlwgt         education        
 Min.   :17.00   Length:32561       Min.   :  12285   Length:32561      
 1st Qu.:28.00   Class :character   1st Qu.: 117827   Class :character  
 Median :37.00   Mode  :character   Median : 178356   Mode  :character  
 Mean   :38.58                      Mean   : 189778                     
 3rd Qu.:48.00                      3rd Qu.: 237051                     
 Max.   :90.00                      Max.   :1484705                     
 education_num     marital           occupation        relationship      
 Min.   : 1.00   Length:32561       Length:32561       Length:32561      
 1st Qu.: 9.00   Class :character   Class :character   Class :character  
 Median :10.00   Mode  :character   Mode  :character   Mode  :character  
 Mean   :10.08                                                           
 3rd Qu.:12.00                                                           
 Max.   :16.00                                                           
     race               sex             capital_gain    capital_loss   
 Length:32561       Length:32561       Min.   :    0   Min.   :   0.0  
 Class :character   Class :character   1st Qu.:    0   1st Qu.:   0.0  
 Mode  :character   Mode  :character   Median :    0   Median :   0.0  
                                       Mean   : 1078   Mean   :  87.3  
                                       3rd Qu.:    0   3rd Qu.:   0.0  
                                       Max.   :99999   Max.   :4356.0  
  hr_per_week      country             income         
 Min.   : 1.00   Length:32561       Length:32561      
 1st Qu.:40.00   Class :character   Class :character  
 Median :40.00   Mode  :character   Mode  :character  
 Mean   :40.44                                        
 3rd Qu.:45.00                                        
 Max.   :99.00                                        
> str(adult)
'data.frame':	32561 obs. of  15 variables:
 $ age          : int  39 50 38 53 28 37 49 52 31 42 ...
 $ type_employer: chr  "State-gov" "Self-emp-not-inc" "Private" "Private" ...
 $ fnlwgt       : int  77516 83311 215646 234721 338409 284582 160187 209642 45781 159449 ...
 $ education    : chr  "Bachelors" "Bachelors" "HS-grad" "11th" ...
 $ education_num: int  13 13 9 7 13 14 5 9 14 13 ...
 $ marital      : chr  "Never-married" "Married-civ-spouse" "Divorced" "Married-civ-spouse" ...
 $ occupation   : chr  "Adm-clerical" "Exec-managerial" "Handlers-cleaners" "Handlers-cleaners" ...
 $ relationship : chr  "Not-in-family" "Husband" "Not-in-family" "Husband" ...
 $ race         : chr  "White" "White" "White" "Black" ...
 $ sex          : chr  "Male" "Male" "Male" "Male" ...
 $ capital_gain : int  2174 0 0 0 0 0 0 0 14084 5178 ...
 $ capital_loss : int  0 0 0 0 0 0 0 0 0 0 ...
 $ hr_per_week  : int  40 13 40 40 40 40 16 45 50 40 ...
 $ country      : chr  "United-States" "United-States" "United-States" "United-States" ...
 $ income       : chr  "<=50K" "<=50K" "<=50K" "<=50K" ...
> table(adult$type_employer)

               ?      Federal-gov        Local-gov     Never-worked 
            1836              960             2093                7 
         Private     Self-emp-inc Self-emp-not-inc        State-gov 
           22696             1116             2541             1298 
     Without-pay 
              14 
> unemp <- function(job){
+   job <- as.character(job)
+   if(job== 'Never-worked' | job=='Without-pay'){
+     return('Unemployed')
+   }else{
+     return(job)
+   }
+ }
> adult$type_employer <- sapply(adult$type_employer,unemp)
> group_emp <- function(job){
+   if (job=='Local-gov' | job=='State-gov'){
+     return('SL-gov')
+   }else if (job=='Self-emp-inc' | job=='Self-emp-not-inc'){
+     return('self-emp')
+   }else{
+     return(job)
+   }
+ }
> adult$type_employer <- sapply(adult$type_employer,group_emp)
> table(adult$type_employer)

          ? Federal-gov     Private    self-emp      SL-gov  Unemployed 
       1836         960       22696        3657        3391          21 
> status.m <- function(married){
+   if(married=='Married-spouse-absent' | married == 'Married-AF-spouse' | married== 'Married-civ-spouse'){
+     return('Married')
+   }else if (married=='Separated' | married=='Divorced' | married=='Widowed'){
+     return('Not-Married')
+   }else{
+     return(married)
+   }
+ }
> adult$marital <- sapply(adult$marital,status.m)
> table(adult$marital)

      Married Never-married   Not-Married 
        15417         10683          6461 
> Asia <- c('China','Hong','India','Iran','Cambodia','Japan', 'Laos' ,
+           'Philippines' ,'Vietnam' ,'Taiwan', 'Thailand')
> 
> North.America <- c('Canada','United-States','Puerto-Rico' )
> 
> Europe <- c('England' ,'France', 'Germany' ,'Greece','Holand-Netherlands','Hungary',
+             'Ireland','Italy','Poland','Portugal','Scotland','Yugoslavia')
> 
> Latin.and.South.America <- c('Columbia','Cuba','Dominican-Republic','Ecuador',
+                              'El-Salvador','Guatemala','Haiti','Honduras',
+                              'Mexico','Nicaragua','Outlying-US(Guam-USVI-etc)','Peru',
+                              'Jamaica','Trinadad&Tobago')
> Other <- c('South')
> group_country <- function(ctry){
+   if (ctry %in% Asia){
+     return('Asia')
+   }else if (ctry %in% North.America){
+     return('North.America')
+   }else if (ctry %in% Europe){
+     return('Europe')
+   }else if (ctry %in% Latin.and.South.America){
+     return('Latin.and.South.America')
+   }else{
+     return('Other')      
+   }
+   adult$country <- sapply(adult$country,group_country)
+ table(adult$country)
+   table(adult$country)

that is the output in the console as you notice I tried to use table function 2 times and that is the result.

it rewrites the code without any results

As I said, you are missing a closing bracket in your last function definition (group_country), that is why the console stays idle waiting for you to "finish" the command.

Please read the guide on the link I gave you and try to post a proper reproducible example including sample data on a copy/paste friendly format

1 Like

If you just type the missing brackets into the console the functions will finish. The "+" on your last line just indicates, that your loops and functions are not correctly closed.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.