[R] Dataframes and text identifier columns

PIKAL Petr petr.pikal at precheza.cz
Thu Jul 3 09:48:43 CEST 2014


Hi

What is the problem?

some comments in line

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Brian Willis
> Sent: Wednesday, July 02, 2014 1:33 PM
> To: r-help at r-project.org
> Subject: Re: [R] Dataframes and text identifier columns
>
> Apologies I was trying to simplify the programme and missed out four
> input files. The files on Andrew, Burt, Charlie  and Dave have the same
> format of one factor and 13 numeric variables with repeated
> measurements eg.
> Study v1      v2      v3      v4      v5      v6      v7      v8      v9      v10     v11
>       v12     v13
> A     153     4.0     2.00    2.00    145.00  0.67    0.01    49.00   0.34    0.04
>       0.96    -3.24   0.04
> B     96      33      3.0     13.0    47.0    0.9     0.2     4.2     0.1     0.5     0.5
>       -0.7    -0.7
>
> Inp_dat is
> Case  r       p       SE      n
> Andrew        0.03    0.01    0.0004  500
> Burt  0.08    0.111   0.04    50
> Charlie       0.04    0.022   0.0005  200
> Dave  0.2     0.028   0.006   85
>
> out_put starts as empty data frame and rows are added incrementally one
> for Andrew, one for Burt etc.
> If the code is
> Andrew<-read.csv("/File /Andrew.csv")
> Burt<-read.csv("/File /Burt.csv")
> Charlie<-read.csv("/File /Charlie.csv")
> Dave<-read.csv("/File /Dave.csv")
>
> Inp_dat<- read.csv("/File/Input data.csv")
>
>
> out_put<-data.frame(Case=character(), StdL=numeric(), StdPP=numeric(),
> StdSE=numeric(), L=numeric(), MRPP=numeric(), MRSE=numeric(),
> stringsAsFactors=FALSE)
>
> for(i in 1:4)
> {
> if (i==1) b<-Andrew
> if (i==2) b<-Burt
> if (i==3) b<-Charlie
> if (i==4) b<-Dave
^^^^^^^^^^^^^^^^^
you do not use b in your further code so this is not necessary

>
> pr <- Inp_dat$p[i]
> SE_pr <- Inp_dat$SE[i]
> r<- Inp_dat$r[i]
> n<- Inp_dat$n[i]
> Case<- Inp_dat$Case[i]
>>
> out_put[i,]<-data.frame(Case, stdL, stdPP, stdSE, L, PP, PP_SE)
>
> }
> out_put
>
>   Case     StdL          StdPP           StdSE           L
> MRPP            MRSE
> 1    1  19.466823   0.16432300   0.03137456   26.002294   0.2080145
> 0.03804692
> 2    2   2.334130    0.22566939   0.08962662    5.095703    0.3888451
> 0.08399101
> 3    3   2.588678    0.05502765   0.00454159   42.058326   0.4861511
> 0.02128030
> 4    4   7.857898    0.18457822   0.04372297    4.705487    0.1193687
> 0.01921609
>
>
>
> The Cases are labelled as integers 1 corresponding to Andrew, 2
> corresponding to Burt etc. instead of the intended text labels Andrew,
> Burt, Charlie and Dave.

If you want to change Case to labels just use

out_put$Case <- factor(out_put$Case), labels(Inp_dat$Case))

Regards
Petr

>
> Note all other columns are correct. Furthermore
>
> str(Case)
> Factor w/ 4 levels "Andrew","Burt",..: 4
>
> str(out_put)
>
> 'data.frame':   4 obs. of  7 variables:
>  $ Case  : chr  "1" "2" "3" "4"
>  $ StdL : num  19.47 2.33 2.59 7.86
> etc
>
>
> I have tried changing the line
>
> Case<- Inp_dat$Case[i]
>  to
>
> Case<- levels(Inp_dat$Case)[i]
>
> and this gives the following output
>
>   Case     StdL          StdPP           StdSE           L
> MRPP            MRSE
> 1    1  19.466823   0.16432300   0.03137456   26.002294   0.2080145
> 0.03804692
> 2    1   2.334130    0.22566939   0.08962662    5.095703    0.3888451
> 0.08399101
> 3    1   2.588678    0.05502765   0.00454159   42.058326   0.4861511
> 0.02128030
> 4    1   7.857898    0.18457822   0.04372297    4.705487    0.1193687
> 0.01921609
>
> str(Case)
>
> chr "Dave"
>
> and
>
> str(out_put)
>
> 'data.frame':   4 obs. of  7 variables:
>  $ Case  : chr  "1" "1" "1" "1"
>  $ StdL : num  19.47 2.33 2.59 7.86
> etc
>
>
> I’ve also tried adding, as suggested the stringsAsFactors=FALSE to the
> Inp_dat<- read.csv("/File/Input data.csv", stringsAsFactors=FALSE)
>
> This gives the same as the 2nd output above.
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Dataframes-
> and-text-identifier-columns-tp4693184p4693389.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.

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


More information about the R-help mailing list