melt

Separate string after last underscore

给你一囗甜甜゛ 提交于 2019-12-07 15:28:57
问题 This is indeed a duplicate for this question r-split-string-using-tidyrseparate, but I cannot use the MWE for my purpose, because I do not know how to adjust the regular Expression. I basically want the same thing, but split the variable after the last underscore. Reason: I have data where some columns show up several times for the same factor/type. I figured I can melt the data separate the value variable before the type string and spread it out again to a wide format with less columns. My

dcast with custom fun.aggregate

做~自己de王妃 提交于 2019-12-06 12:05:49
I have data that looks like this: sample start end gene coverage X 1 10 A 5 X 11 20 A 10 Y 1 10 A 5 Y 11 20 A 10 X 1 10 B 5 X 11 20 B 10 Y 1 10 B 5 Y 11 20 B 10 I added additional columns: data$length <- (data$end - data$start + 1) data$ct_lt <- (data$length * data$coverage) I reformated my data using dcast: casted <- dcast(data, gene ~ sample, value.var = "coverage", fun.aggregate = mean) So my new data looks like this: gene X Y A 10.00000 10.00000 B 38.33333 38.33333 This is the correct data format I desire, but I would like to fun.aggregate differently. Instead, I would like to take a

R: Collapse multiple boolean columns into single attribute column with new rows for each combination [duplicate]

一个人想着一个人 提交于 2019-12-06 03:29:58
This question already has an answer here: Converting multiple boolean columns to single factor column 4 answers Trying to melt or collapse a dataframe with multiple boolean columns into a two column database with an id column and a column for the collapsed values BUT each value results in a new row. Example beginning: A S1 S2 S3 S4 1 ex1 1 0 0 0 2 ex2 0 1 0 0 3 ex3 0 0 1 0 4 ex4 1 1 0 0 5 ex5 0 1 0 1 6 ex6 0 1 0 0 7 ex7 1 1 1 0 8 ex8 0 1 1 0 9 ex9 0 0 1 0 10 ex10 1 0 0 0 Desired output: A Type ex1 S1 ex2 S2 ex3 S3 ex4 S1 ex4 S2 ex5 S2 ex5 S4 ex6 S2 ex7 S1 ex7 S2 ex7 S3 ex8 S2 ex8 S3 ex9 S3

Separate string after last underscore

荒凉一梦 提交于 2019-12-06 01:27:39
This is indeed a duplicate for this question r-split-string-using-tidyrseparate , but I cannot use the MWE for my purpose, because I do not know how to adjust the regular Expression. I basically want the same thing, but split the variable after the last underscore. Reason: I have data where some columns show up several times for the same factor/type. I figured I can melt the data separate the value variable before the type string and spread it out again to a wide format with less columns. My Problem is that my variable names have different several underscores and I would like to learn how to

Reshape data frame from wide to long with re-occuring column names in R

♀尐吖头ヾ 提交于 2019-12-05 22:07:24
I'm trying to convert a data frame from wide to long format using the melt formula. The challenge is that I have multiple column names that are labeled the same. When I use the melt function, it drops the values from the repeat column. I have read similar questions and it was advised to use the reshape function, however I was not able to get it work. To reproduce my starting data frame: conversion.id<-c("1", "2", "3") interaction.num<-c("1","1","1") interaction.num2<-c("2","2","2") conversion.id<-as.data.frame(conversion.id) interaction.num<-as.data.frame(interaction.num) interaction.num2<-as

How can I add rows for all dates between two columns?

半城伤御伤魂 提交于 2019-12-05 07:05:46
import pandas as pd mydata = [{'ID' : '10', 'Entry Date': '10/10/2016', 'Exit Date': '15/10/2016'}, {'ID' : '20', 'Entry Date': '10/10/2016', 'Exit Date': '18/10/2016'}] mydata2 = [{'ID': '10', 'Entry Date': '10/10/2016', 'Exit Date': '15/10/2016', 'Date': '10/10/2016'}, {'ID': '10', 'Entry Date': '10/10/2016', 'Exit Date': '15/10/2016', 'Date': '11/10/2016'}, {'ID': '10', 'Entry Date': '10/10/2016', 'Exit Date': '15/10/2016', 'Date': '12/10/2016'}, {'ID': '10', 'Entry Date': '10/10/2016', 'Exit Date': '15/10/2016', 'Date': '13/10/2016'}, {'ID': '10', 'Entry Date': '10/10/2016', 'Exit Date':

Reshape/gather function to create dataset ready for multilevel analysis

送分小仙女□ 提交于 2019-12-04 02:13:42
问题 I have a big dataset, with 240 cases representing 240 patients. They all have undergone neuropsychological tests and filled in questionnaires. Additionally, their significant others (hereafter: proxies) have also filled in questionnaires. Since 'patient' and 'proxy' are nested in 'couples', I want to conduct a multilevel analysis in R. For this, I need to reshape my dataset to run those kind of analysis. Simply said, I want to 'duplicate' my rows. For the double subject IDs add a new variable

How to melt and cast dataframes using dplyr?

[亡魂溺海] 提交于 2019-12-03 01:59:53
问题 Recently I am doing all my data manipulations using dplyr and it is an excellent tool for that. However I am unable to melt or cast a data frame using dplyr. Is there any way to do that? Right now I am using reshape2 for this purpose. I want 'dplyr' solution for: require(reshape2) data(iris) dat <- melt(iris,id.vars="Species") 回答1: The successor to reshape2 is tidyr . The equivalent of melt() and dcast() are gather() and spread() respectively. The equivalent to your code would then be library

R - Reshape - Melt Error

十年热恋 提交于 2019-12-02 20:54:12
问题 I am trying to melt a data frame and I get this weird error. Any ideas why? str(zx7) 'data.frame': 519 obs. of 5 variables: $ calday.new: Date, format: "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" ... $ A20 : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... $ B20 : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... $ C20 : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... $ D20 : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... zx7.melt <- melt(zx7, id=c("calday.new")) Error in `[

How to melt and cast dataframes using dplyr?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 15:29:27
Recently I am doing all my data manipulations using dplyr and it is an excellent tool for that. However I am unable to melt or cast a data frame using dplyr. Is there any way to do that? Right now I am using reshape2 for this purpose. I want 'dplyr' solution for: require(reshape2) data(iris) dat <- melt(iris,id.vars="Species") The successor to reshape2 is tidyr . The equivalent of melt() and dcast() are gather() and spread() respectively. The equivalent to your code would then be library(tidyr) data(iris) dat <- gather(iris, variable, value, -Species) If you have magrittr imported you can use