dplyr

replace Yes, No to 1, 0 in multiple columns in r [duplicate]

风格不统一 提交于 2021-02-17 07:03:34
问题 This question already has answers here : Converting Yes and No to 0 and 1 in R (3 answers) Closed last year . I need to replace the Yes and No values to 1 and 0 in 200 columns. I was using with the command data %>% mutate(x=ifelse(x=="Yes", 1,0)) but i have to go through each column at a time, and i was wondering if there was any way i could do it all at once. 回答1: You may use ifelse on the entire data frame: df <- data.frame(v1=c("Yes","No"), v2=c("No","Yes"), stringsAsFactors=FALSE) ifelse

R: Titles on Graphs not Updating

自作多情 提交于 2021-02-17 06:53:26
问题 I am working with the R programming language. I am trying to replicate the following tutorial with some fake data that I generated: https://plotly.com/r/dropdowns/. That is, I generated some fake data and made 4 scatter plots. Using the "plotly" library, I then want to "attach" these 4 plots together and let the user "toggle" (switch, shuffle) between these graphs. I have attached the code below: #load libraries library(plotly) library(MASS) library(dplyr) # create data x <- sample( LETTERS[1

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

dplyr filtering on multiple columns using “%in%”

徘徊边缘 提交于 2021-02-17 06:38:14
问题 I have a dataframe (df1) with multiple columns (ID, Number, Location, Field, Weight). I also have another dataframe (df2) with more information (ID, PassRate, Number, Weight). I am trying to use dplyr and %in% to filter out rows in df1 that have the same two values as df2. So far I have: df_sub <- subset(df1, df1$ID %in% df2$ID & df1$Weight %in% df2$Weight) But this is only subsetting on the first condition...any idea why? 回答1: From the question and sample code, it is unclear whether you want

Summarising by a group variable in r

点点圈 提交于 2021-02-17 06:31:31
问题 I have a data frame as follows: head(newStormObject) FATALITIES INJURIES PROPVALDMG CROPVALDMG EVTYPE total 1 0 15 2.5e+05 0 TORNADO 15 2 0 0 2.5e+04 0 TORNADO 0 3 0 3 2.5e+07 0 TORNADO 3 4 0 3 2.5e+07 0 TORNADO 3 5 0 0 0.0e+00 0 TSTM WIND 1 6 0 0 0.0e+00 0 HAIL 2 7 0 0 0.0e+00 0 HAIL 3 8 0 0 0.0e+00 0 TSTM WIND 0 9 0 0 0.0e+00 0 HAIL 0 10 0 0 0.0e+00 0 TSTM WIND 0 11 0 0 0.0e+00 0 TSTM WIND 0 12 0 0 0.0e+00 0 HAIL 1 13 0 0 0.0e+00 0 HAIL 1 14 0 0 0.0e+00 0 HAIL 5 15 0 0 0.0e+00 0 TSTM WIND 0

Summarising by a group variable in r

十年热恋 提交于 2021-02-17 06:31:27
问题 I have a data frame as follows: head(newStormObject) FATALITIES INJURIES PROPVALDMG CROPVALDMG EVTYPE total 1 0 15 2.5e+05 0 TORNADO 15 2 0 0 2.5e+04 0 TORNADO 0 3 0 3 2.5e+07 0 TORNADO 3 4 0 3 2.5e+07 0 TORNADO 3 5 0 0 0.0e+00 0 TSTM WIND 1 6 0 0 0.0e+00 0 HAIL 2 7 0 0 0.0e+00 0 HAIL 3 8 0 0 0.0e+00 0 TSTM WIND 0 9 0 0 0.0e+00 0 HAIL 0 10 0 0 0.0e+00 0 TSTM WIND 0 11 0 0 0.0e+00 0 TSTM WIND 0 12 0 0 0.0e+00 0 HAIL 1 13 0 0 0.0e+00 0 HAIL 1 14 0 0 0.0e+00 0 HAIL 5 15 0 0 0.0e+00 0 TSTM WIND 0

How to check multiple values using if condition [duplicate]

▼魔方 西西 提交于 2021-02-17 05:31:47
问题 This question already has answers here : Idiom for ifelse-style recoding for multiple categories (12 answers) Closed 2 years ago . I have like below mentioned dataframe: Records: ID Remarks Value 1 ABC 10 1 AAB 12 1 ZZX 15 2 XYZ 12 2 ABB 14 By utilizing the above mentioned dataframe, I want to add new column Status in the existing dataframe. Where if the Remarks is ABC, AAB or ABB than status would be TRUE and for XYZ and ZZX it should be FALSE . I am using below mentioned method for that but

How to compare the rows of two dataframes in R

杀马特。学长 韩版系。学妹 提交于 2021-02-17 03:36:51
问题 I'm trying to compare two columns of different data frames to create a new data frame. If the value of the row of the first col is less than the second, it will add a 1 to the new column. When the value is greater, it will add a 2 and so on. I'll give you an example. I have this df df1 <- data.frame(col=c(1,seq(1:9),9,10)) # col # 1 1 # 2 1 # 3 2 # 4 3 # 5 4 # 6 5 # 7 6 # 8 7 # 9 8 # 10 9 # 11 9 # 12 10 And this one, which has less rows df2<-data.frame(col2=c(3,6,8)) # col2 # 1 3 # 2 6 # 3 8

Merge 2 dataframes using conditions on “hour” and “min” of df1 in datetimes of df2

旧街凉风 提交于 2021-02-16 20:07:29
问题 I have a dataframe df.sample like this id <- c("A","A","A","A","A","A","A","A","A","A","A") date <- c("2018-11-12","2018-11-12","2018-11-12","2018-11-12","2018-11-12", "2018-11-12","2018-11-12","2018-11-14","2018-11-14","2018-11-14", "2018-11-12") hour <- c(8,8,9,9,13,13,16,6,7,19,7) min <- c(47,59,6,18,22,36,12,32,12,21,47) value <- c(70,70,86,86,86,74,81,77,79,83,91) df.sample <- data.frame(id,date,hour,min,value,stringsAsFactors = F) df.sample$date <- as.Date(df.sample$date,format="%Y-%m-