All Questions
140
questions
0
votes
1
answer
71
views
How to iterate a function for multiple values (Loop function)?
By running the following function, the output would be:
library(pmsampsize)
pmsampsize(type = "s", csrsquared = 0.5, parameters = 10, rate = 0.065,
timepoint = 2, meanfup = 2.07)
NB: ...
0
votes
1
answer
55
views
Color code values conditionally in xlsx produced by openxlsx package in R in a loop
My reproducible code below writes a super small .xlsx file that has two sheets.
I was wondering if there might be a way to color-code values of 3 in red, 4 in orange, and 5 in green anywhere in the ...
1
vote
1
answer
75
views
Create list of unique dataset combinations of duplicate removal
I have a dataset with duplicates across groups.
For instance:
dat <- data.frame(
group = c("A", "A", "A", "B", "B", "C","C",&...
1
vote
2
answers
58
views
subtract all unique pairs of elements of a list in R
In My_list below, I want to subtract all unique pairs of elements (no repetition) from one another.
In this example, because there are 3 elements in My_list, therefore, we expect choose(3, 2)= three ...
0
votes
2
answers
39
views
Iterating over a loop with two vectors of different lengths
I have two dataframes:
harv<-structure(list(spp = c("Other species (please list species name; i.e. Tarpon)",
"Other species (please list species name; i.e. Amberjack)", "...
1
vote
2
answers
83
views
Column binding vectors of unequal length in tidyverse
Below, I'm trying to col-bind the input to create a data.frame using the map_dfc() to acheive my Desired_output.
But it looks like map_dfc() can only col-bind equally-lengthed vectors.
So, I wonder ...
0
votes
2
answers
51
views
subset variable names in R using more than one condition
I have the following code:
df <- data.frame(
check.names = FALSE,
`Att-Bissen P [mm]` = c(57937.8),
`Att-Bissen PET [mm]` = c(39472.9),
`Att-Bissen Q [mm]` = c(26501.2),
`Rau. Merl P [mm]`...
0
votes
1
answer
14
views
How do I retain an object within function
I'm trying to create a function that creates an object and then loops through and binds everything together. The error I've found is that:
In rm(long) : object 'long' not found
long appears to cease ...
1
vote
1
answer
325
views
Pulling data out of multiple tabs in excel sheets
I have nearly 300 excel sheets in an untidy format that makes it difficult to import data. In the picture below, you'll see an Excel sheet that has three tabs. Each tab is in the same format as the ...
0
votes
1
answer
119
views
Use purrr to iterate over a vector of values to generate lagged values
I'd like to use some of the purrr functions to create a vector of lagged values in a column.
I've a dataframe as follows:
df <- data.frame(
x = sample(1000, size = 100)
)
I can create a new ...
0
votes
1
answer
175
views
purrr map: index a vector with elements of a list
I am trying to change my code from base-r for-loops to purrr. I want to access a variable number of elements from an integer vector for a list with k elements.
I have a vector
set.seed(1)
subset_from&...
0
votes
0
answers
41
views
Loops and functions
I have created a loop that iterates through consecutive dates in an SQL query. The aim is to create a table in R from the SQL output (bigquery). This works by storing the query as a string in R and ...
0
votes
1
answer
130
views
Mutate numerical column name with respect to another variable R
I have a tibble with the following structure:
In that tibble there are a vast amount of lines, the ids are unique and for each one of them there is at least one value different from NA (in the column ...
0
votes
2
answers
122
views
Efficiently concatenate tibble of outputs from for-loop
I have some complex analysis code in a for loop and am trying to compile the outputs in a single dataframe / tibble. Following the answer here I am avoiding modifying the main output tibble within the ...
0
votes
1
answer
157
views
How do I create a loop or an array to calculate risk ratios for five different variables?
I'm calculating risk ratios for several different foods after an outbreak at a picnic (I've made up the data for privacy reasons). The dataset I have is shown below as a tibble:
# A tibble: 7 × 7
...