r

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

replace NA with 0 and all other values/text as 1

拈花ヽ惹草 提交于 2021-02-17 06:39:35
问题 airquality Ozone Solar.R Wind Temp Month Day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12 149 12.6 74 5 3 4 18 313 11.5 62 5 4 5 NA NA 14.3 56 5 5 6 28 NA 14.9 66 5 6 7 23 299 8.6 65 5 7 8 19 99 13.8 59 5 8 9 8 19 20.1 61 5 9 Hi there, How do I replace values in Ozone to be binary? If NA then 0 and if a value then 1. Thanks H 回答1: Assuming your dataframe is called airquality airquality$Ozone <- ifelse(is.na(airquality$Ozone), 0, 1) 回答2: airquality$Ozone <- as.integer(!is.na(airquality$Ozone))

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

R Getting JSON data into dataframe

陌路散爱 提交于 2021-02-17 06:37:27
问题 I have this file with JSON formatted data, but need this into a dataframe. Ultimately I would like to plot the geolocations onto a map, but can't seem to get this data into a df first. json_to_df <- function(file){ file <- lapply(file, function(x) { x[sapply(x, is.null)] <- NA unlist(x) }) df <- do.call("rbind", file) return(df) } But I get only this error: Error in fromJSON(file) : STRING_ELT() can only be applied to a 'character vector', not a 'list' The file structure looks like this (this

R Getting JSON data into dataframe

泪湿孤枕 提交于 2021-02-17 06:37:13
问题 I have this file with JSON formatted data, but need this into a dataframe. Ultimately I would like to plot the geolocations onto a map, but can't seem to get this data into a df first. json_to_df <- function(file){ file <- lapply(file, function(x) { x[sapply(x, is.null)] <- NA unlist(x) }) df <- do.call("rbind", file) return(df) } But I get only this error: Error in fromJSON(file) : STRING_ELT() can only be applied to a 'character vector', not a 'list' The file structure looks like this (this

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

replace text using values from lookup table without for loop

半城伤御伤魂 提交于 2021-02-17 06:31:08
问题 I'm writing a function for spelling correction. I scraped spelling variants page from wikipedia and converted it into a table. I want to now use this as lookup table (spellings) and replace values in my documents (skills.db). NOTE: skills data frame below is just an example. ignore the second column. I will be performing the spelling correction much earlier in the process on resumes. resumes are large, so i thought I'll share this instead. I can do this using a for loop as below, however I'm

Inverse of ggplotGrob?

三世轮回 提交于 2021-02-17 06:30:10
问题 I have a function which manipulates a ggplot object, by converting it to a grob and then modifying the layers. I would like the function to return a ggplot object not a grob. Is there a simple way to convert a grob back to gg? The documentation on ggplotGrob is awfully sparse. Simple example: P <- ggplot(iris) + geom_bar(aes(x=Species, y=Petal.Width), stat="identity") G <- ggplotGrob(P) ... some manipulation to G ... ## DESIRED: P2 <- inverse_of_ggplotGrob(G) such that, we can continue to use