I'm working in R & using a for loop to iterate through my data:
pos = c(1256:1301,6542:6598)
sd_all = null
for (i in pos){
nameA = paste("A", i, sep = "_")
nameC = paste("C", i, sep = "_")
resA = assign(nameA, unlist(lapply(files, function(x) x$percentageA[x$position==i])))
resC = assign(nameC, unlist(lapply(files, function(x) x$percentageC[x$position==i])))
sd_A = sd(resA)
sd_C = sd(resC)
sd_all = ?
}
now I want to generate a vector called 'sd_all' that contains the standard deviations of resA & resC. I cannot just do 'sd_all = c(sd(resA), sd(resC))', because then I only use one value in 'pos'. I want to do it for all values in 'pos' off course.
It looks like you'd be best served with sd_all as a list object. That way you can insert each of your 2 values ( sd(resA) and sd(resC) ).
Initialising a list is simple (this would replace the second line of your code):
sd_all <- list()
Then you can insert both the values you want to into a single list element like so (this would replace the last line in your for loop):
sd_all[[ i ]] <- c( sd( resA ), sd( resC ) )
After your loop, you can then insert this list as a column in a data.frame if that's what you'd like to do.
Related
I need to compare a character string to multiple others and tried to do it the following way:
empty = character(0)
ps_2 = c("h2","h3")
ps_3 = c("h3", "h4")
visible = ("h2")
i = 2
ps_t = empty
ps_t <- append(ps_t, sum(visible %in% paste("ps_", i, sep="")))
With the intention to write a loop instead of i = 2, in order to cycle trough ps_2,ps_3,...
However I think it's not working since the paste() command returns a string instead of the character string with the name: ps_2.
How can I fix this?
Thanks for the time and effort!
Kind regards,
A fellow datafanatic!
The function you need is get(), which gets the value of the object.
ps_t <- ps_t = NULL
sapply(2:3, function(i) append(ps_t, sum(visible %in% get(paste0("ps_", i)))))
Or simply:
sapply(2:3, function(i) sum(visible %in% get(paste0("ps_", i))))
Output
[1] 1 0
You can use eval in R to convert the string to a variable name. You can find the solution here.
Here's what your code will look like:
ps_t <- c(0, (sum(visible %in% eval(parse(text = paste("ps_", i, sep=""))))))
It will give you a numeric vector.
OR
You can use get.
ps_t <- append(0, sum(visible %in% get(paste("ps_", i, sep = ""))))
ps_t
I have the following code which takes keywords from the user so that I can search a database of word_vectors:
theme_terms = lemmatize_words(scan(strip.white = TRUE, sep = '\n', what = character()))
I want to use the words provided in the following assignment, but the user can enter as many words as they like, and my current assignment is unfortunately static.
result = word_vectors[theme_terms[1], , drop = F] + word_vectors[theme_terms[2], , drop = F]
I want to recreate the above statement in a for loop that will take as many words as the user inputs.
I have tried the following:
a) doesnt work as I am trying to bind a non-numeric argument to a binary operator
for(i in 1:length(theme_terms)){
temp = word_vectors[theme_terms[i], , drop = F]
result = result + temp
}
b) doesnt work as its a string and I also have to remove string marks and extra + at the end
for(i in 1:length(theme_terms)){
temp = paste0(paste0("word_vectors[", theme_terms[i],", , drop = F] + "))
result = paste(result, temp)
}
Any suggestions? thank you.
edit.
word_vectors is a matrix of values which can be reproduced as follows:
terms = c("cancer", "blood", "machine")
v1 = c(0.002, 0.313, 0.1313)
v2 = c(0.23, 0.14, 0.155)
v3 = c(0.141, 0.41, 0.125)
word_vectors = as.matrix(data.frame(terms, v1, v2, v3))
Use sapply :
result <- sapply(theme_terms, function(x) word_vectors[x, , drop = F])
With for loop we can initialize a list for storing the output
out <- vector('list', length(theme_terms))
for(i in seq_along(theme_terms)) {
out[[i]] <- word_vectors[theme_terms[i], , drop = FALSE]
}
I'm looping through a vector with prefixes. I'm assigning dataframes in the loop, based on the prefix and I want to add them to a list.
This is the code I have. It works to initialize the dataframe and they also get the correct names. But how can I add them to the list
prefix = c("green", "red", "orange")
diff_list = list()
for (i in 1:length(prefix)) {
tmp_name = paste(prefix[i], "_diff_tbl", sep = "")
assign(tmp_name, data.frame())
diff_list[prefix[i]] = ???
}
I am not sure if the code below reaches your goal
diff_list <- setNames(lapply(prefix,function(x) data.frame()),paste(prefix,"_diff_tbl", sep = ""))
and
list2env(setNames(lapply(prefix,function(x) data.frame()),paste(prefix,"_diff_tbl", sep = "")),envir = .GlobalEnv)
You can use rep() or replicate() to copy an object many times and store them into a list.
## option 1
diff_list <- setNames(rep(list(data.frame()), length(prefix)), paste0(prefix, "_diff_tbl"))
and
## option 2
diff_list <- setNames(replicate(length(prefix), data.frame()), paste0(prefix, "_diff_tbl"))
I am new to R and have a question not knowing how to solve it. Maybe you can help?
I do have a separated name/value input string: param1=test;param2=3;param3=140;
I would like to access a value via it's name in R.
Something like using
myParams["param1]
I already tried something like:
input = "param1=test;param2=3;param3=140;"
output1 = strsplit(input,";")[[1]]
output2 = do.call(rbind, strsplit(output1, "="))
to get a matrix but am missing the rest..
You could define a custom function myParams:
# Your sample data
input = "param1=test;param2=3;param3=140;"
output1 = strsplit(input,";")[[1]]
output2 = do.call(rbind, strsplit(output1, "="))
# Define function
myParams <- function(par, df = output2) {
return(df[which(df[, 1] == par), 2])
}
myParams("param1");
#[1] "test"
myParams("param2");
#[1] "3"
A simple way would be to create a dataframe out of that matrix first and then access the value via row names
input = "param1=test;param2=3;param3=140;"
output1 = strsplit(input,";")[[1]]
output2 = do.call(rbind, strsplit(output1, "="))
temp = data.frame(output2,row.names = TRUE)
# X2
#param1 test
#param2 3
#param3 140
temp[,"param1"]
#test
temp[,"param2"]
#3
temp[,"param3"]
#140
I have the following data table and function which extracts a parameter and adds it as a column to a data table:
library(stringr)
library(data.table)
pq <- c("cm_mmc=MSN-_-Exact-_-BrandExact-_-CheddarCheese"
,"cm_mmc=Google-_-cheeseTypes-_-cheddar-_-cheesedelivered&gclid=CMXyy2D81MsCFcsW0w0dMVoPGw"
,"cm_mmc=MSN-_-worldcitiesuslocations-_-cheese-_-cheeseshops"
,"cm_mmc=MSN-_-worldcitiesuslocations-_-cheese-_-cheeseshops")
rq <- c("q=cheese&src=IE-SearchBox&FORM=IESR02",
"sa=L",
"q=london+cheese+shop&src=IE-TopResult&FORM=IETR02&pc=WCUG",
"q=london+cheese+shop&src=IE-TopResult&FORM=IETR02&pc=WCUG")
DT = data.table(page_urlquery = pq, refr_urlquery = rq)
# Extracts a paramater from the relevant query and adds it to the table as a column
extract_param <- function(dt, source = c("page_urlquery", "refr_urlquery"), param_name){
source <- match.arg(source)
regexp <- paste("(?i)", param_name, "=([^&]+)", sep="")
col_name <- switch(source
,"page_urlquery" = paste("url_", param_name, sep = "")
,"refr_urlquery" = paste("ref_", param_name, sep = "")
)
dt[,(col_name):= str_match((source), regexp)[,2]]
}
However when I call the function as follows:
extract_param(DT, "page_urlquery", "cm_mmc")
It creates the column, but the contents are blank. I think it's something wrong with the syntax in the data table (source) parameter. What am I missing?
change the code inside function from
dt[,(col_name):= str_match((source), regexp)[,2]]
to
dt[,(col_name):= str_match(get(source), regexp)[,2]]