[R] restructuring data frame

PIKAL Petr petr.pikal at precheza.cz
Fri Mar 10 14:56:41 CET 2017


Thanks Ulrik.

separate_rows is interesting, but it results in data frame with rows with offending numbers stored in V4 variable.

> separate_rows(evid, V4, sep = ";#")
                                               V2         V3                 V4
1                        test vodivosti kalcinátu 03.03.2017       EICHLER Věra
2                        test vodivosti kalcinátu 03.03.2017                125
3 impregnace anatasové pasty rozprašovací sušárna 17.03.2017 HOŠŤÁLKOVÁ Jarmila
4 impregnace anatasové pasty rozprašovací sušárna 17.03.2017                119
5 impregnace anatasové pasty rozprašovací sušárna 17.03.2017    BERNÁT Miroslav
6 impregnace anatasové pasty rozprašovací sušárna 17.03.2017                122
7 impregnace anatasové pasty rozprašovací sušárna 17.03.2017      OSTRČIL Marek
8 impregnace anatasové pasty rozprašovací sušárna 17.03.2017                 60

I could get rid of them and select only odd rows by
evid[liche(1:nrow(evid)),]

So it is easier than my solution, which is slight expansion of my first attempt, using my convenience function liche for selecting only odd values.

mena<-lapply(strsplit(evid[,3], ";#"), liche)
temp <- evid[rep(seq_len(nrow(evid)),  unlist(lapply(mena, length))),-3]
cbind(temp, mena=unlist(mena))

Cheers
Petr


From: Ulrik Stervbo [mailto:ulrik.stervbo at gmail.com]
Sent: Friday, March 10, 2017 10:26 AM
To: PIKAL Petr <petr.pikal at precheza.cz>; r-help at r-project.org
Subject: Re: [R] restructuring data frame

Hi Petr,
maybe

library("splitstackshape")
cSplit(evid, "V4", "#", direction = "long")

or

library("tidyr")
separate_rows(evid, V4, sep = "#")

is helpful.

Best,
Ulrik

On Fri, 10 Mar 2017 at 08:32 PIKAL Petr <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>> wrote:
Dear all

I have some data with following structure in data frame.

dput(evid[1:2,c(2:4)])

evid <- structure(list(V2 = c("test vodivosti kalcinátu", "impregnace anatasové pasty rozprašovací sušárna"
), V3 = c("03.03.2017", "17.03.2017"), V4 = c("EICHLER Věra;#125",
"HOŠŤÁLKOVÁ Jarmila;#119;#BERNÁT Miroslav;#122;#OSTRČIL Marek;#60"
)), .Names = c("V2", "V3", "V4"), row.names = 9:10, class = "data.frame")

Each row in V4 column contain names followed by ;#xxx. I would like to separate them like that

mena <- liche(unlist(strsplit(evid[2,4], ";#")))

here is function liche

liche <- function (x)
{
    indices <- seq(along = x)
    x[indices%%2 == 1]
}

and repeat each respective row of data frame for separated names like following (which is only for one row)

temp<-evid[2,][rep(seq_len(nrow(evid[2,])), length(mena)),-4]
cbind(temp, mena)
           V1                                              V2         V3
10   NEPRAVDA impregnace anatasové pasty rozprašovací sušárna 17.03.2017
10.1 NEPRAVDA impregnace anatasové pasty rozprašovací sušárna 17.03.2017
10.2 NEPRAVDA impregnace anatasové pasty rozprašovací sušárna 17.03.2017
                V5            V6         V7               mena
10   OSTRČIL Marek OSTRČIL Marek 1kalcinace HOŠŤÁLKOVÁ Jarmila
10.1 OSTRČIL Marek OSTRČIL Marek 1kalcinace    BERNÁT Miroslav
10.2 OSTRČIL Marek OSTRČIL Marek 1kalcinace      OSTRČIL Marek

I probably could do it for each row in cycle (the data frame is not big) but I wonder if somebody knows any more elegant/easy/effective solution for such task.

Best regards

Petr Pikal

"Kdo vždy myslí, že se učí,
bude vlasti chlouba.
Kdo si myslí, že dost umí,
začíná být trouba."
Karel Havlíček Borovský



________________________________
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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list