tidyr

Expanding columns associated with a categorical variable into multiple columns with dplyr/tidyr while retaining id variable [duplicate]

≯℡__Kan透↙ 提交于 2019-12-11 07:39:55
问题 This question already has an answer here : R spreading multiple columns with tidyr [duplicate] (1 answer) Closed last year . I have a data.frame that looks like this: dfTall <- frame_data( ~id, ~x, ~y, ~z, 1, "a", 4, 5, 1, "b", 6, 5, 2, "a", 5, 4, 2, "b", 1, 9) I want to turn it into this: dfWide <- frame_data( ~id, ~y_a, ~y_b, ~z_a, ~z_b, 1, 4, 6, 5, 5, 2, 5, 1, 4, 9) Currently, I'm doing this dfTall %>% split(., .$x) %>% mapply(function(df,name) {df$x <- NULL; names(df) <- paste(names(df),

Confusion with Spread in tidyr

℡╲_俬逩灬. 提交于 2019-12-11 07:27:31
问题 I have a dataframe as follows: ddd <- structure(list(sample_date = structure(c(1400612280, 1400612280, 1400612280, 1400612280, 1400612280, 1400612280, 1400616420, 1400616420, 1400616420, 1400616420, 1400616420, 1400616420, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780

How to ignore cor.test:“not enough finite observations” and continue, when using tidyverse and ggplot2 (ggpmisc)

怎甘沉沦 提交于 2019-12-11 07:12:01
问题 I have the following working-toy example: trunctiris <- iris [1:102,] analysis <- trunctiris %>% group_by(Species) %>% nest() %>% mutate(model = map(data, ~lm(Sepal.Length ~ Sepal.Width, data = .)), cor = map(data, ~tidy(cor.test(.x$Sepal.Length, .x$Sepal.Width), 3))) stats <- analysis %>% unnest(cor) ggplot(trunctiris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(shape = 21) + geom_text(data = stats, aes(label = sprintf("r = %s", round(estimate, 3)), x = 7, y = 4)) + geom_text(data =

Issue with pivot_longer and pivot_wider

瘦欲@ 提交于 2019-12-11 06:06:37
问题 I am trying to use pivot_longer and pivot_wider and it works fine in a stand alone script. But as soon as I use this in shiny I get the following error: Warning: Values in `value` are not uniquely identified; output will contain list-cols. * Use `values_fn = list(value = list)` to suppress this warning. * Use `values_fn = list(value = length)` to identify where the duplicates arise * Use `values_fn = list(value = summary_fun)` to summarise duplicates Warning: Error in : Can't cast `x` <list

How do I separate every character in a string in a vector into a column using tidyr

你。 提交于 2019-12-11 06:05:13
问题 I want to separate each string in the vector into columns but I can't do it! library(tidyr) library(dplyr) df <- data.frame(x = c("abe", "bas", "dds", "eer")) df %>% separate(x, c("A", "B", "C"), sep=1) The output I want looks like this A B C 1 a b e 2 b a s 3 d d s 4 e e r That sep=1 works for 2 characters but doesn't work for 3. I was hoping a regex like sep="." or sep="[a-z]" would work too but it doesn't. This is probably super easy but I'm new to R. Won't someone please help! 回答1: You

Apply tidyr::separate over multiple columns

僤鯓⒐⒋嵵緔 提交于 2019-12-11 05:13:51
问题 I would like to iterate over columns in a dataframe and split them into the based on a separator. I am using tidyr::separate , which works when I do one column at a time. For example: df<- data.frame(a = c("5312,2020,1212"), b = c("345,982,284")) df <- separate(data = df, col = "a", into = paste("a", c("col1", "col2", "col3"), sep = "_"), sep = ",") Returns: a_col1 a_col2 a_col3 b 1 5312 2020 1212 345,982,284 When I try to execute the same operation over each column of df R returns an error

`gather` can't handle rownames

泪湿孤枕 提交于 2019-12-11 05:06:10
问题 allcsvs = list.files(pattern = "*.csv$", recursive = TRUE) library(tidyverse) ##LOOP to redact the snow data csvs## for(x in 1:length(allcsvs)) { df = read.csv(allcsvs[x], check.names = FALSE) newdf = df %>% gather(COL_DATE, SNOW_DEPTH, -PT_ID, -DATE) %>% mutate( DATE = as.Date(DATE,format = "%m/%d/%Y"), COL_DATE = as.Date(COL_DATE, format = "%Y.%m.%d") ) %>% filter(DATE == COL_DATE) %>% select(-COL_DATE) ####TURN DATES UNAMBIGUOUS HERE#### df$DATE = lubridate::mdy(df$DATE) finaldf = merge

Applying tidyr separate only to specific rows

拜拜、爱过 提交于 2019-12-11 04:12:52
问题 I'm trying to use tidyr to separate one column in my data frame, while applying it only to specific rows. While dplyr::filter does the job, it omits the rest of my data. Is there a clean way to apply tidyr to specific rows while keeping the rest of the data untouched? here is an example of my problem: #creating DF for the example df<-data.frame(var_a=letters[1:5], var_b=c(sample(1:100,5)), text=c("foo_bla","here_do","oh_yes","baa","land")) gives me this: var_a var_b text 1 a 10 foo_bla 2 b 58

Tidyr Separate using regex

混江龙づ霸主 提交于 2019-12-11 03:33:36
问题 I searched and searched for this and found similar stuff but nothing quite right. Hopefully this hasn't been answered. Lets say I have a column with Y,N, and sometimes extra information df<-data.frame(Names=c("Patient1","patient2","Patient3","Patient4","patient5"),Surgery=c("Y","N","Y-this kind of surgery","See note","Y")) And I'm trying to separate out the Y or N into one column, and everything else from that column into another. I've tried df%>%separate('Surgery',c("Surgery","Notes"), sep="

Applying a function for multiple groups using dplyr

匆匆过客 提交于 2019-12-11 00:57:25
问题 I have some data for multiple location and year big.data <- data.frame(loc.id = rep(1:3, each = 10*3), year = rep(rep(1981:1983, each = 10),times = 3), day = rep(1:10, times = 3*3), CN = rep(c(50,55,58), each = 10*3), top.FC = rep(c(72,76,80),each = 10*3), DC = rep(c(0.02,0.5,0.8), each = 10*3), WAT0 = rep(c(20,22,26), each = 10*3), Precp = sample(1:100,90, replace = T), ETo = sample(1:10,90, replace = T)) I have a function: water.model which uses a second function internally called water