splitstackshape

How to create new columns in a data.frame based on row values in R?

≡放荡痞女 提交于 2021-01-28 19:30:40
问题 Hej, I have a data.frame with family trios, and I would like to add a column with the full sibs of every "id" (= offspring). My data: df id dam sire 1: 83295 67606 79199 2: 83297 67606 79199 3: 89826 67606 79199 What I would like to retrieve: df2 id dam sire fs1 fs2 1: 83295 67606 79199 83297 89826 2: 83297 67606 79199 83295 89826 3: 89826 67606 79199 83295 83297 What I’ve tried: (similar to: How to transform a dataframes row into columns in R?) library(dplyr) library(splitstackshape) df2 <-

How to create new columns in a data.frame based on row values in R?

邮差的信 提交于 2021-01-28 19:25:46
问题 Hej, I have a data.frame with family trios, and I would like to add a column with the full sibs of every "id" (= offspring). My data: df id dam sire 1: 83295 67606 79199 2: 83297 67606 79199 3: 89826 67606 79199 What I would like to retrieve: df2 id dam sire fs1 fs2 1: 83295 67606 79199 83297 89826 2: 83297 67606 79199 83295 89826 3: 89826 67606 79199 83295 83297 What I’ve tried: (similar to: How to transform a dataframes row into columns in R?) library(dplyr) library(splitstackshape) df2 <-

cSplit library(splitstackshape) is always dropping the column

倖福魔咒の 提交于 2020-01-03 08:27:36
问题 I was searching for a way to split the column content by a separator and converting a table into a long format. I found cSplit from the splitstackshape package and it is almost doing what I was looking for. Problem is now with the drop option. I expected my split column to be copied in a way, but this does not happen. Am I doing it wrong? Somebody experienced the problem? I am not sure if I do something wrong, but the drop = FALSE option is not working in my case. Here is an example: library

R separate comma separated cells into rows and Cartesian product

徘徊边缘 提交于 2019-12-20 03:52:04
问题 I have mydf data frame below. I want to split any cell that contains comma separated data and put it into rows. I am looking for a data frame similar to y below. How could i do it efficiently in few steps? Currently i am using cSplit function on one column at a time. I tried cSplit(mydf, c("name","new"), ",", direction = "long") , but that didn`t work library(splitstackshape) mydf=data.frame(name = c("AB,BW","x,y,z"), AB = c('A','B'), new=c("1,2,3","4,5,6,7")) mydf x=cSplit(mydf, c("name"), "

Splitting a single column into multiple observation using R

一曲冷凌霜 提交于 2019-12-04 03:09:10
问题 I am working on HCUP data and this has range of values in one single column that needs to be split into multiple columns. Below is the HCUP data frame for reference : code label 61000-61003 excision of CNS 0169T-0169T ventricular shunt The desired output should be : code label 61000 excision of CNS 61001 excision of CNS 61002 excision of CNS 61003 excision of CNS 0169T ventricular shunt My approach to this problem is using the package splitstackshape and using this code library(data.table)

R separate comma separated cells into rows and Cartesian product

跟風遠走 提交于 2019-12-02 03:57:16
I have mydf data frame below. I want to split any cell that contains comma separated data and put it into rows. I am looking for a data frame similar to y below. How could i do it efficiently in few steps? Currently i am using cSplit function on one column at a time. I tried cSplit(mydf, c("name","new"), ",", direction = "long") , but that didn`t work library(splitstackshape) mydf=data.frame(name = c("AB,BW","x,y,z"), AB = c('A','B'), new=c("1,2,3","4,5,6,7")) mydf x=cSplit(mydf, c("name"), ",", direction = "long") x y=cSplit(x, c("new"), ",", direction = "long") y There are times when a for

Splitting a single column into multiple observation using R

白昼怎懂夜的黑 提交于 2019-12-01 16:27:11
I am working on HCUP data and this has range of values in one single column that needs to be split into multiple columns. Below is the HCUP data frame for reference : code label 61000-61003 excision of CNS 0169T-0169T ventricular shunt The desired output should be : code label 61000 excision of CNS 61001 excision of CNS 61002 excision of CNS 61003 excision of CNS 0169T ventricular shunt My approach to this problem is using the package splitstackshape and using this code library(data.table) library(splitstackshape) cSplit(hcup, "code", "-")[, list(code = code_1:code_2, by = label)] This

Create several dummy variables from one string variable

自作多情 提交于 2019-11-29 15:51:06
I've tried pretty much everything from this similar question , but I can't get the results everyone else seems to be getting. This is my problem: I have a data frame like this, listing the grades each teacher works with: > profs <- data.frame(teaches = c("1st", "1st, 2nd", "2nd, 3rd", "1st, 2nd, 3rd")) > profs teaches 1 1st 2 1st, 2nd 3 2nd, 3rd 4 1st, 2nd, 3rd I've been looking for solutions to break the teaches variable into columns, like so: teaches1st teaches2nd teaches3rd 1 1 0 0 2 1 1 0 3 0 1 1 4 1 1 1 I understand this solution involving the splitstackshape library and the apparently

Displaying data in the chart based on plotly_click in R shiny

我的未来我决定 提交于 2019-11-28 14:50:39
Please run this script below, the following R script gives a shiny dashboard with two boxes. I want to reduce the width between two boxes and display data in the right chart. The data should be based on the on click event that we see in the ggplotly function. Also plotly can be used to do the job, I guess. I want the code to fast and efficient at the same time. ## app.R ## library(shiny) library(shinydashboard) library(bupaR) library(eventdataR) library(lubridate) library(dplyr) library(XML) library(edeaR) library(xml2) library(data.table) library(ggplot2) library(ggthemes) library(glue)

Create several dummy variables from one string variable

邮差的信 提交于 2019-11-28 10:10:01
问题 I've tried pretty much everything from this similar question, but I can't get the results everyone else seems to be getting. This is my problem: I have a data frame like this, listing the grades each teacher works with: > profs <- data.frame(teaches = c("1st", "1st, 2nd", "2nd, 3rd", "1st, 2nd, 3rd")) > profs teaches 1 1st 2 1st, 2nd 3 2nd, 3rd 4 1st, 2nd, 3rd I've been looking for solutions to break the teaches variable into columns, like so: teaches1st teaches2nd teaches3rd 1 1 0 0 2 1 1 0