Problem: Code will not run past ##ERROR FROM HERE. After setting the vetor to 100 iterations of 40 words, I think r is struggling to mutate the words in ctrl_d and run the formula on them (word*1000/ dictionary size). Could any advise as to why this is? In the error code below, 'stretch' is one of the 40 random words in ctrl_d dataframe.
R error output:
Error: Problem with mutate()
input stretch
. x non-numeric argument to binary operator i Input stretch
is unit * stretch/dictsize
.
Context code:
#new control data frame which removes 16 starting rain tokens
nouncount_ctrl <- nouncount %>% select(-matches(rainset_regex))
#1) restrict dictionaries to total noun counts of 1000 only
nouncount_ctrl <- nouncount_ctrl %>% filter(total_ncount >= 1000)
#convert 0 values to NA
nouncount_ctrl[nouncount_ctrl == 0] <- NA
#2) filter for appearance of tokens in more than 5% of dictionaries (words must appear in >97 dictionaries)
nouncount_ctrl = nouncount_ctrl[,!sapply(nouncount_ctrl, function(x) mean(is.na(x)))>0.95]
#generate vector for number of control trials
vector <- c(1:100)
#package to print correlation statistics
magic_for(print, silent = TRUE)
#loop control analysis, iterating for vector length
for(number in vector){
#select random words
random_words <- colnames(sample(select(nouncount_ctrl, 11:3240), size=40))
#generate initial control dataframe
ctrl_d <- nouncount_ctrl %>%
select(id, random_words, dictsize) %>% mutate_all(~replace_na(.x, 0))
unit <- 1000
Code causing error:
# mutate relative frequencies
for (word in random_words){
ctrl_d <- ctrl_d %>% mutate(!!sym(word) := unit * !!sym(word)/dictsize)
}