All Questions
76 questions
1
vote
3
answers
57
views
For every instance if "A" in Column1, create a new column with all values associated with "A" from Column2
Here is an example data frame:
df <- data.frame(Key = c(rep("A", 2), rep("B", 4), rep("C", 3), rep("D", 2)),
DataID = round(runif(11, min = ...
1
vote
1
answer
83
views
Looping through the rows of a dataframe to create a cumulative sum
library(tidyverse)
set.seed(1)
start <- mdy("01/01/2022")
end <- start + as.difftime(99, units = "days")
days <- seq(from = start, to = end, by = 1)
days <- sample(days,...
0
votes
2
answers
74
views
Fill values into column of data frame based on two conditions
I have a dataframe where I would like to fill in values from column "reference column" into the column "trial_number" based on the following two conditions:
condition 1: value ...
1
vote
1
answer
306
views
R dataframe: filling specific number of rows above and below where conditions are met
I am working in R, I have a dataframe with a DateTime column and a Binary column marking an event in time, as in the sample dataframe:
DateTime <- seq(from = as.POSIXct("2021-01-01 00:00:00&...
0
votes
1
answer
41
views
For loop with conditions in a function R
I would like to print something for every variable in a dataset. If the variable is a factor class, then I would like to print "variable x belongs in table 1", else "variable x belongs ...
4
votes
2
answers
370
views
How to conditionally change the iterator value in for loop?
I created the following example in an attempt to clarify my question. ranPick draws a random number for each iteration but if it draws a negative number the next two places are also assigned the same ...
1
vote
1
answer
175
views
skip an iteration in for-loop in case of an error message
I am writing a code to scrape data from websites in R where I'm bound to get an error saying no such directory was found.
It is a part of a longer for-loop and in case of error when the loop usually ...
0
votes
1
answer
519
views
Creating a new variable adding 1 each time a condition is met in R
I am working with a survey data set in which each observation (respondents) is represented by an own row. I want to create a new (numeric) variable which counts the number of times a condition is met ...
1
vote
4
answers
610
views
How to store only the first occurrence in a for loop?
I tried to code k as a cumulatively increasing variable until it reaches 15. j is supposed to store the point when k exceeds 10 for the first time. However, obviously, j is going to store all the ...
4
votes
4
answers
124
views
How to find and replace values in a df according to a list of priority words (with for loop and condition)?
I have a column in a data frame with multiple words in each cell separated by ";" (second column).
my_dataframe <- data.frame( first_column = c("x", "y", "x",...
0
votes
1
answer
32
views
Update values conditionally in for loop
I wondered why I can't replicate the first example with a for loop. More specifically you can see that if we try to execute the logic with a for loop the second and third row are not populated with a ...
0
votes
1
answer
149
views
Loop of mutual information in R
I have a table with zero and ones in R like this
m <- 10
n <- 5
dat <- round(matrix(runif(m * n), m, n))
resulting in:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 1 1 1 0
[2,] ...
0
votes
2
answers
89
views
Modify values based on condition in R
I have the following dataframe:
x income
1 46200
2 89345
3 189982
4 255465
5 301189
6 18100
7 55234
8 ...
0
votes
2
answers
64
views
If conditions nested in for loop not executing
I've put together the following code snippet. It will run, without error, but the "StandPlotTree_ID_Past" variable is unchanged, in any row.
I'm not sure how the code is escaping the loop ...
0
votes
1
answer
34
views
If_else statements based on multiple columns that creates a score that is averaged based on how many true statements in R
I have multiple columns that I have put into categories
The data is in categories such as:
Q1 Q2 - TS
Q3 - NI
Q4 Q5 Q6 - SALES
I want to create a column for each category that gives a score
for ...