conditional-statements

dplyr: Replace values rowwise based on value in one variable

我是研究僧i 提交于 2021-02-17 06:40:14
问题 I want to exclude participants from an analysis that are too old (age >90). Usually I would do it like that: df <- data.frame(age=c(1,10, 100), x= 1:3, y= 1:3) df[df$age > 90, ] <- NA I can't figure out how to do this with dplyr. If we want to replace one variable we can use library(dplyr) df <- data.frame(age=c(1,10, 100), x= 1:3, y= 1:3) df %>% mutate(age= replace(age, age> 90, NA)) So I thought I could use df %>% mutate_all(function(i) replace(i, age> 90, NA)) I also tried mutate_if and

dplyr: Replace values rowwise based on value in one variable

喜你入骨 提交于 2021-02-17 06:40:12
问题 I want to exclude participants from an analysis that are too old (age >90). Usually I would do it like that: df <- data.frame(age=c(1,10, 100), x= 1:3, y= 1:3) df[df$age > 90, ] <- NA I can't figure out how to do this with dplyr. If we want to replace one variable we can use library(dplyr) df <- data.frame(age=c(1,10, 100), x= 1:3, y= 1:3) df %>% mutate(age= replace(age, age> 90, NA)) So I thought I could use df %>% mutate_all(function(i) replace(i, age> 90, NA)) I also tried mutate_if and

Multiply and replace values in data frame according to condition in R

笑着哭i 提交于 2021-02-17 02:52:08
问题 I'm new to R and I've been trying to multiply and replace certain values in my data frame with no success. Basically, what I want to do is that when a value from my df (any column) is 0 < x < 1, multiplicate it by 10 and then replace that value with the result of this equation. A glimpse to my df just in case... 'data.frame': 404 obs. of 15 variables: $ D3: num 16.1 17.1 16.1 16.1 17.2 ... $ TH : num 9.9 8.6 9.7 7.7 7.6 7.6 8.7 9.8 9.8 7.7 ... $ D2 : num 33.3 29.3 30.3 29.3 33.3 ... $ D1 :

How to filter rows based on the previous row and keep previous row using dplyr?

无人久伴 提交于 2021-02-15 07:50:40
问题 I am trying to subset rows of a data set using a condition that's based on the previous row, whilst keeping the previous row in the subsetted data. This is essentially the same as the question here, but I am looking for a dplyr approach: Select specific rows based on previous row value (in the same column) I have taken the dplyr approach applied in the comments to that answer, but I am unable to figure out the last step of retaining the previous row. I can get the rows that support the

Add new value to new column based on if value exists in other dataframe in R

主宰稳场 提交于 2021-02-13 05:38:47
问题 I have two dataframes called users and purchases with thousands of datasets to each. Both have a feature called ID . My aim is to add a new column called buyer to the dataframe purchases , if the value of ID of purchases exists in ID of users . So the two dataframes look like this: users = data.frame("ID" = c(23432,75645,5465645,5656,6456)) purchases = data.frame("ID" = c(6456,4436,88945)) It should look like: 回答1: We can use %in% to compare the values and wrap as.integer to convert logical

Bash: conditional based on specific exit code of command

纵然是瞬间 提交于 2021-02-11 15:59:32
问题 This question is a follow-up to Bash conditional based on exit code of command . I understand, and have made use of the accepted answer to that question: $ cat ./txt foo bar baz $ if ! grep -Fx bax txt &>/dev/null; then echo "not found"; fi not found $ But I can't figure out the syntax to pull off a seemingly simple twist to the premise: how can I write a conditional for a specific exit code of a command? This post pointed out that grep might fail with error code > 1; so I wanted to write the

Typescript: Partial<MyType> with sub-properties to optional

江枫思渺然 提交于 2021-02-11 14:28:01
问题 Is it possible, in typescript, to let a method accept Partial<Something> , in a way that Something 's sub-properties are all set to optional too? export interface ISomething { user: IUser; } export interface IUser { id: number; name: string; } export const myMethod = (something: Partial<ISomething>): void => {}; myMethod({ user: { id: 1, name: "" } }); //this works myMethod({ user: { id: 1 } }); //this doesn't (but I want this to work too) many thanks ;) 回答1: You are essentially looking for

Replace the same values in the consecutive rows and stop replacing once the value has changed in R

那年仲夏 提交于 2021-02-11 12:47:56
问题 I want to find a way to replace consecutive same values into 0 at the beginning of each trial, but once the value has changed it should stop replacing and keep the value. It should occur every trials per subject. For example, first subject has multiple trials (1, 2, etc). At the beginning of each trial, there may be some consecutive rows with the same value (e.g., 1, 1, 1). For these values, I would like to replace them to 0. However, once the value has changed from 1 to 0, I want to keep the

Replace the same values in the consecutive rows and stop replacing once the value has changed in R

╄→гoц情女王★ 提交于 2021-02-11 12:47:06
问题 I want to find a way to replace consecutive same values into 0 at the beginning of each trial, but once the value has changed it should stop replacing and keep the value. It should occur every trials per subject. For example, first subject has multiple trials (1, 2, etc). At the beginning of each trial, there may be some consecutive rows with the same value (e.g., 1, 1, 1). For these values, I would like to replace them to 0. However, once the value has changed from 1 to 0, I want to keep the

Counting cases, two conditions, range of values

早过忘川 提交于 2021-02-11 12:31:24
问题 I already found out how to count cases based on two conditions: sum(dat2$happy > 7 & dat2$induction == 1) But do you know how I can include a range of values as a criterion? One out of many codes I tried: sum(dat2[dat2$happy > 4 & dat2$happy < 8] & dat2$induction == 2) Tank you so much! 来源: https://stackoverflow.com/questions/65923147/counting-cases-two-conditions-range-of-values