+34 616 71 29 85 carsten@dataz4s.com

Logic statements and cbind in R

Logic statements can be used obtain values as TRUE/FALSE and 0/1 for categorical values. With cbind we can add columns. This page is part of My notes on R programming.

Read in data

# Read in data via read_excel
library(readxl)
LungCapData <- read_excel(“C:/Users/Usuario/Documents/dataZ4s/R/MarinLectures/LungCapData.xlsx”,
                          col_types = c(“numeric”, “numeric”, “numeric”,
                                        “text”, “text”, “text”))

# R reads in Smoke, Gender and Caesarean as “text”. Needs change to “factor”
# Change Smoke, Gender and Caesarean to factors with as.factor() command
LungCapData$Smoke <- as.factor(LungCapData$Smoke)
LungCapData$Gender <- as.factor(LungCapData$Gender)
LungCapData$Caesarean <- as.factor(LungCapData$Caesarean)

# attach(LungCapData)
attach(LungCapData)

Checking

# Checking names
head(LungCapData)

## # A tibble: 6 x 6
##   LungCap   Age Height Smoke Gender Caesarean
##     <dbl> <dbl>  <dbl> <fct> <fct>  <fct>   
## 1    6.48     6   62.1 no    male   no      
## 2   10.1     18   74.7 yes   female no      
## 3    9.55    16   69.7 no    female yes     
## 4   11.1     14   71   no    male   no      
## 5    4.8      5   56.9 no    male   no      
## 6    6.22    11   58.7 no    female no

Age[1:5]

## [1]  6 18 16 14  5

Logic statements as TRUE/FALSE and 0/1

# Is the age more than 14
# Shown by TRUE/FALSE
# 2nd, 3rd and 9th rows are > 14
temp <- Age>14
temp[1:10]

##  [1] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE

# Replacing TRUE/FALSE with 0/1
# Change to numeric with as.numeric command
temp2 <- as.numeric(Age>15)
temp2[1:10]

##  [1] 0 1 1 0 0 0 0 0 0 0

cbind to add column

# View the first 10 rows
LungCapData[1:10, ]

## # A tibble: 10 x 6
##    LungCap   Age Height Smoke Gender Caesarean
##      <dbl> <dbl>  <dbl> <fct> <fct>  <fct>   
##  1    6.48     6   62.1 no    male   no      
##  2   10.1     18   74.7 yes   female no      
##  3    9.55    16   69.7 no    female yes     
##  4   11.1     14   71   no    male   no      
##  5    4.8      5   56.9 no    male   no      
##  6    6.22    11   58.7 no    female no      
##  7    4.95     8   63.3 no    male   yes     
##  8    7.32    11   70.4 no    male   no      
##  9    8.88    15   70.5 no    male   no      
## 10    6.8     11   59.2 no    male   no

# Having Gender and Smoke returned as logic statements
FemSmoke <- Gender==”female” & Smoke==”yes”
FemSmoke[1:10]

##  [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

# Adding column with cbind
# Add column with TRUE/FALSE
ExtraColumn <- cbind(LungCapData, FemSmoke)
ExtraColumn[1:10, ]

##    LungCap Age Height Smoke Gender Caesarean FemSmoke
## 1    6.475   6   62.1    no   male        no    FALSE
## 2   10.125  18   74.7   yes female        no     TRUE
## 3    9.550  16   69.7    no female       yes    FALSE
## 4   11.125  14   71.0    no   male        no    FALSE
## 5    4.800   5   56.9    no   male        no    FALSE
## 6    6.225  11   58.7    no female        no    FALSE
## 7    4.950   8   63.3    no   male       yes    FALSE
## 8    7.325  11   70.4    no   male        no    FALSE
## 9    8.875  15   70.5    no   male        no    FALSE
## 10   6.800  11   59.2    no   male        no    FALSE

This page is a run through of Statslectures with Mick Marin on his video ‘Logic Statements…’. View this page in my site: https://dataz4s.com/r-statistical-programming/logic-statements-cbind-r/

Carsten Grube

Carsten Grube

Freelance Data Analyst

0 Comments

+34 616 71 29 85

Call me

Spain: Ctra. 404, km 2, 29100 Coín, Malaga

...........

Denmark: c/o Musvitvej 4, 3660 Stenløse

Drop me a line

What are you working on just now? Can I help you, and can you help me? 

About me

Learning statistics. Doing statistics. Freelance since 2005. Dane. Living in Spain. With my Spanish wife and two children. 

What they say

20 years in sales, analysis, journalism and startups. See what my customers and partners say about me.