Re: [R] reshape data frame when one column has unequal number of entries
Hi Dimitri here is a quick crude way (needs some polishing) data.frame(a = rep(x$a,sapply(sapply(x$b, strsplit, , ), length)), b= unlist(sapply(x$b, strsplit, , ))) Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mac...@northnet.com.au -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Dimitri Liakhovitski Sent: Thursday, 23 April 2015 23:15 To: r-help Subject: [R] reshape data frame when one column has unequal number of entries Hello! I have my data frame x with 2 character columns: x - data.frame(a = numeric(), b = I(list())) x[1:3,a] = 1:3 x[[1, b]] - a, b, c x[[2, b]] - d, e x[[3, b]] - f x$a = as.character(x$a) x$b = as.character(x$b) x str(x) I need to produce this data frame: 1 a 1 b 1 c 2 d 2 e 3 f Is it possible without looping? Thank you! -- Dimitri Liakhovitski __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] reshape data frame when one column has unequal number of entries
Hi I am not sure if this is more efficient than some loop I just gave your data another column names. names(x)-c(one, two) x one two 1 1 a, b, c 2 2d, e 3 3 f s-(strsplit(x$two, ,)) s [[1]] [1] a b c [[2]] [1] d e [[3]] [1] f first-rep(x$one ,unlist(lapply(s, length))) data.frame(first, second=unlist(s)) first second 1 1 a 2 1 b 3 1 c 4 2 d 5 2 e 6 3 f Maybe you want to remove extra white space form your values. It is mentioned somewhere in help pages to regular expressions Cheers Petr -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Dimitri Liakhovitski Sent: Thursday, April 23, 2015 3:15 PM To: r-help Subject: [R] reshape data frame when one column has unequal number of entries Hello! I have my data frame x with 2 character columns: x - data.frame(a = numeric(), b = I(list())) x[1:3,a] = 1:3 x[[1, b]] - a, b, c x[[2, b]] - d, e x[[3, b]] - f x$a = as.character(x$a) x$b = as.character(x$b) x str(x) I need to produce this data frame: 1 a 1 b 1 c 2 d 2 e 3 f Is it possible without looping? Thank you! -- Dimitri Liakhovitski __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům. Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému. Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu. V případě, že je tento e-mail součástí obchodního jednání: - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu. - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou. - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech. - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] reshape data frame when one column has unequal number of entries
Thank you very much, everybody! On Thu, Apr 23, 2015 at 10:38 AM, Duncan Mackay dulca...@bigpond.com wrote: Hi Dimitri here is a quick crude way (needs some polishing) data.frame(a = rep(x$a,sapply(sapply(x$b, strsplit, , ), length)), b= unlist(sapply(x$b, strsplit, , ))) Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mac...@northnet.com.au -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Dimitri Liakhovitski Sent: Thursday, 23 April 2015 23:15 To: r-help Subject: [R] reshape data frame when one column has unequal number of entries Hello! I have my data frame x with 2 character columns: x - data.frame(a = numeric(), b = I(list())) x[1:3,a] = 1:3 x[[1, b]] - a, b, c x[[2, b]] - d, e x[[3, b]] - f x$a = as.character(x$a) x$b = as.character(x$b) x str(x) I need to produce this data frame: 1 a 1 b 1 c 2 d 2 e 3 f Is it possible without looping? Thank you! -- Dimitri Liakhovitski __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitri Liakhovitski __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] reshape data frame when one column has unequal number of entries
Hello! I have my data frame x with 2 character columns: x - data.frame(a = numeric(), b = I(list())) x[1:3,a] = 1:3 x[[1, b]] - a, b, c x[[2, b]] - d, e x[[3, b]] - f x$a = as.character(x$a) x$b = as.character(x$b) x str(x) I need to produce this data frame: 1 a 1 b 1 c 2 d 2 e 3 f Is it possible without looping? Thank you! -- Dimitri Liakhovitski __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] reshape data frame
Thanks, that seems to work. On Fri, Nov 15, 2013 at 10:26 PM, arun kirshna [via R] ml-node+s789695n4680556...@n4.nabble.com wrote: Hi, Try: var1 - load(reshape_data.frame.RData) ##It is better not to name the objects with function names. dat1 - data reshape1 - reshape names(dat1)[grep(X\\d+,names(dat1))] - gsub([[:alpha:]],X_,names(dat1)[grep(X\\d+,names(dat1))]) res1 - reshape(dat1,direction=long,varying=7:ncol(dat1),sep=_) res2 - res1[with(res1,order(Yr,Seas,Flt.Svy,Gender,Part,NSAMP)),-9] row.names(res2) - 1:nrow(res2) colnames(res2) - colnames(reshape1) all.equal(res2,reshape1) #[1] TRUE A.K. Some advice on transforming my data would be appreciated. Attached is an .Rdata image with my examples (reshape_data.frame.Rdata). Within the image are 2 objects: 1) The data object contains an example of my original data format. The columns X20 : X50 are histogram bins, and the rows beneath are the number of observations (ie. counts) within those bins. The other columns; Yr, Seas, Flt.Svy, Gender, Part, NSAMP are observation associated with each bin count. 2) The reshape object is the format I need to transform the data object into. I think the reshape() function is designed for this, and my data object is in wide format, and my resphape object would be in long format. If indeed reshape() is the best way to do this, I'm looking for help in calling the function to transform my data. Many thanks, Tim __ [hidden email] http://user/SendEmail.jtp?type=nodenode=4680556i=0mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/reshape-data-frame-tp4680539p4680556.html To unsubscribe from reshape data frame, click herehttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=4680539code=dHNpcHBlbEBnbWFpbC5jb218NDY4MDUzOXwxMTExOTI5MjI= . NAMLhttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml -- View this message in context: http://r.789695.n4.nabble.com/reshape-data-frame-tp4680539p4680707.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] reshape data frame
Hi, Try: var1 - load(reshape_data.frame.RData) ##It is better not to name the objects with function names. dat1 - data reshape1 - reshape names(dat1)[grep(X\\d+,names(dat1))] - gsub([[:alpha:]],X_,names(dat1)[grep(X\\d+,names(dat1))]) res1 - reshape(dat1,direction=long,varying=7:ncol(dat1),sep=_) res2 - res1[with(res1,order(Yr,Seas,Flt.Svy,Gender,Part,NSAMP)),-9] row.names(res2) - 1:nrow(res2) colnames(res2) - colnames(reshape1) all.equal(res2,reshape1) #[1] TRUE A.K. Some advice on transforming my data would be appreciated. Attached is an .Rdata image with my examples (reshape_data.frame.Rdata). Within the image are 2 objects: 1) The data object contains an example of my original data format. The columns X20 : X50 are histogram bins, and the rows beneath are the number of observations (ie. counts) within those bins. The other columns; Yr, Seas, Flt.Svy, Gender, Part, NSAMP are observation associated with each bin count. 2) The reshape object is the format I need to transform the data object into. I think the reshape() function is designed for this, and my data object is in wide format, and my resphape object would be in long format. If indeed reshape() is the best way to do this, I'm looking for help in calling the function to transform my data. Many thanks, Tim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Reshape data frame with dcast and melt
Hello, I implemented two functions reshape_long and reshape_wide (see full working example below) to reshape data frames. I created several small examples and the two functions seemed to work properly. However, using the reshape_wide function on my real data sets (about 200.000 to 300.000 rows) failed. What happens is set all values for X, Y and Z were set to 1. The structure of my real data looks exactly the same as the small example below. After working on it for 2 days I think the problem is that the primary key (test_name, group_name and id) is only unique in the wide form. After applying the reshape_long function the primary key is not longer unique. I was wondering if anyone can tell me whether the step from d1 - reshape_wide - d2 can work at all because of the non uniqueness of d1. library(reshape2) library(taRifx) reshape_long - function(data, ids) { # Bring data into long form data_long - melt(data, id.vars = ids, variable.name=Data_Points, value.name=value) data_long$value - as.numeric(data_long$value) # Remove rows were analyte value is NA data_long - data_long[!is.na(data_long$value), ] # Resort data formula_sort - as.formula(paste(~, paste(ids, collapse=+))) data_long - sort(data_long, f = formula_sort) return(data_long) } reshape_wide - function(data, ids) { # Bring data into wide form formula_wide - as.formula(paste(paste(ids, collapse=+), ~ Data_Points)) data_wide - dcast(data, formula_wide) # Resort data formula_sort - as.formula(paste(~, paste(ids, collapse=+))) data_wide - sort(data_wide, f = formula_sort) return(data_wide) } d - data.frame( test_name = c(rep(Test_A, 6), rep(Test_B, 6)), group_name = c(rep(Group_C, 3), rep(Group_D, 3), rep(Group_C, 3), rep(Group_D, 3)), id = c(I1, I2, I3, I4, I5, I6, I1, I2, I3, I7, I8, I9), X = c(NA,NA,1,2,3,4,5,6,NA,7,8,9), Y = as.numeric(10:21), Z = c(NA,22,23,NA,24,NA,25,26,NA,27,28,29) ) d d1 - reshape_long(d, ids=c(test_name, group_name, id)) d1 d2 - reshape_wide(d1, ids=c(test_name, group_name, id)) d2 identical(d,d2) -- View this message in context: http://r.789695.n4.nabble.com/Reshape-data-frame-with-dcast-and-melt-tp4484332p4484332.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.