Maho Takahashi

Linguistics PhD Student at UC San Diego

research

CV

code

mtakahas[at]ucsd[dot]edu

How to generate a new column

data <- read.csv('./sampledata.csv')
head(data, 3)
##   Movement Island_Type Island Distance Item
## 1       WH         whe    non       sh    1
## 2       WH         whe    non       sh    2
## 3       WH         whe    non       sh    3
##                                   Sentence Subj_id Score
## 1 Who thinks that Paul stole the necklace?       1     6
## 2     Who thinks that Matt chased the bus?       1     2
## 3 Who thinks that Tom sold the television?       1     3

generate a new column based on an existing column

suppose that I want to create a column Condition consisting of values 1 through 4, which correspond to the conditions as specified under the Island and Distance columns. Here is one way to achieve this.

data <- data %>% mutate(Condition = case_when((Island == "non" & Distance == "sh") ~ 1,
                                              (Island == "non" & Distance == "lg") ~ 2,
                                              (Island == "isl" & Distance == "sh") ~ 3,
                                              (Island == "isl" & Distance == "lg") ~ 4,
                                              TRUE ~ 0))
head(data, 3)[, -5:-1]
##                                   Sentence Subj_id Score Condition
## 1 Who thinks that Paul stole the necklace?       1     6         1
## 2     Who thinks that Matt chased the bus?       1     2         1
## 3 Who thinks that Tom sold the television?       1     3         1

generate a new column and populate it with new values

this code creates a new column called Attachment and populates it with the value “high” for each row.

data$Attachment = rep('high', nrow(data))
head(data, 3)[, -5:-1]
##                                   Sentence Subj_id Score Condition Attachment
## 1 Who thinks that Paul stole the necklace?       1     6         1       high
## 2     Who thinks that Matt chased the bus?       1     2         1       high
## 3 Who thinks that Tom sold the television?       1     3         1       high