reshape2

Complicated reshaping

喜夏-厌秋 提交于 2019-12-29 20:31:19
问题 I want to reshape my dataframe from long to wide format and I loose some data that I'd like to keep. For the following example: df <- data.frame(Par1 = unlist(strsplit("AABBCCC","")), Par2 = unlist(strsplit("DDEEFFF","")), ParD = unlist(strsplit("foo,bar,baz,qux,bla,xyz,meh",",")), Type = unlist(strsplit("pre,post,pre,post,pre,post,post",",")), Val = c(10,20,30,40,50,60,70)) # Par1 Par2 ParD Type Val # 1 A D foo pre 10 # 2 A D bar post 20 # 3 B E baz pre 30 # 4 B E qux post 40 # 5 C F bla pre

R reshape2 'Aggregation function missing: defaulting to length' [duplicate]

烂漫一生 提交于 2019-12-29 05:44:29
问题 This question already has an answer here : dcast error: ‘Aggregation function missing: defaulting to length’ (1 answer) Closed 2 years ago . I have seen this reshape2 several times on SO but haven't seen a solution to my particular problem; I have a dataset like this; head(data) student test score Adam Exam1 80 Adam Exam2 90 John Exam1 70 John Exam2 60 I am trying to cast this to a wide format that looks like this; Student Exam1 Exam2 ........ ExamX Adam 80 90 John 70 60 using; dcast(data

Tidyr how to spread into count of occurrence [duplicate]

喜欢而已 提交于 2019-12-28 06:48:52
问题 This question already has answers here : How do I get a contingency table? (6 answers) Faster ways to calculate frequencies and cast from long to wide (4 answers) Closed last year . Have a data frame like this other=data.frame(name=c("a","b","a","c","d"),result=c("Y","N","Y","Y","N")) How can I use spread function in tidyr or other function to get the count of result Y or N as column header like this name Y N a 2 0 b 0 1 Thanks 回答1: These are a few ways of many to go about it: 1) With library

Unlisting columns by groups

孤街浪徒 提交于 2019-12-28 04:15:13
问题 I have a dataframe in the following format: id | name | logs ---+--------------------+----------------------------------------- 84 | "zibaroo" | "C47931038" 12 | "fabien kelyarsky" | c("C47331040", "B19412225", "B18511449") 96 | "mitra lutsko" | c("F19712226", "A18311450") 34 | "PaulSandoz" | "A47431044" 65 | "BeamVision" | "D47531045" As you see the column "logs" includes vectors of strings in each cell. Is there an efficient way to convert the data frame to the long format (one observation

how to pivot/unpivot (cast/melt) data frame? [duplicate]

只谈情不闲聊 提交于 2019-12-28 02:06:28
问题 This question already has answers here : Reshaping data.frame from wide to long format (7 answers) Closed 3 years ago . How can I 'unpivot' a table? What is the proper technical term for this? UPDATE: The term is called melt I have a data frame for countries and data for each year Country 2001 2002 2003 Nigeria 1 2 3 UK 2 NA 1 And I want to have something like Country Year Value Nigeria 2001 1 Nigeria 2002 2 Nigeria 2003 3 UK 2001 2 UK 2002 NA UK 2003 1 回答1: I still can't believe I beat

Can dcast be used without an aggregate function? [duplicate]

妖精的绣舞 提交于 2019-12-27 22:10:05
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: This R reshaping should be simple, but dcast from reshape2 works without a formula where there are no duplicates. Take these example data: df <- structure(list(id = c("A", "B", "C", "A", "B", "C"), cat = c("SS", "SS", "SS", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 224L, 225L, 2206L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, -6L)) I'd like to dcast these data and just have the

Using Reshape Cast [duplicate]

痞子三分冷 提交于 2019-12-25 08:16:09
问题 This question already has answers here : How to reshape data from long to wide format (11 answers) Closed 2 years ago . I am trying to reshape an example dataframe by the following. df<-data.frame(market = c("a","b","c","a","b","c"),companyName = c("foo","foo","foo", "bar","bar","bar"), val = seq(1,6)) require(reshape) dfNew <- cast(df,market ~ companyName+companyName) To generate: market company 1 company 2 1 a 1 4 2 b 2 5 3 c 3 6 But I get this error: Using val as value column. Use the

Error with large numerics in dcast.data.table

浪子不回头ぞ 提交于 2019-12-25 07:30:59
问题 Given a data frame I am trying to cast from long-to-wide using the dcast.data.table function from library(data.table) . However, when using large numeric's on the left side of the formula it some how combines. Below is an example: df <- structure(list(A = c(10000000007624, 10000000007619, 10000000007745, 10000000007624, 10000000007767, 10000000007729, 10000000007705, 10000000007711, 10000000007784, 10000000007745, 10000000007624, 10000000007762, 10000000007762, 10000000007631, 10000000007762,

Calculate mean by group [duplicate]

不羁的心 提交于 2019-12-24 18:59:17
问题 This question already has answers here : Aggregate / summarize multiple variables per group (e.g. sum, mean) (6 answers) Calculate the mean by group (4 answers) Closed 4 years ago . set.seed(2218) exdf <- data.frame( c(rep(1:105, 28)), sort(c(rep(1:28, 105))), sort(rep(rnorm(28), 105)), sample(0:1, 105*28, replace=TRUE), rep(rnorm(105), 28) ) colnames(exdf) <- c("ID", "content", "b", "APMs", "Gf") View(exdf) This gives you a good idea of my dataset. Now I would like to turn it into something

how to use melt and dcast on tough data frame

余生颓废 提交于 2019-12-24 14:18:50
问题 I have a data frame that has one value in each cell, but my last column is a list. Example. Here there are 3 columns. X and Y columns have one value in each row. But column Z is actually a list. It can have multiple values in each cell. X Y Z 1 a d h, i, j 2 b e j, k 3 c f l, m, n, o I need to create this: X Y Z 1 a d h 2 a d i 3 a d j 4 b e j 4 b e k 5 c f l 6 c f m 7 c f n 8 c f o Can someone help me figure this out ? I am not sure how to use melt or dcast or any other function for this.