Skip to content

Dataframes and text identifier columns

4 messages · Brian Willis, PIKAL Petr

#
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

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. 

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.
#
Hi

What is the problem?

some comments in line
^^^^^^^^^^^^^^^^^
you do not use b in your further code so this is not necessary
If you want to change Case to labels just use

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

Regards
Petr
________________________________
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.
#
Thank you for the suggestion

What seems to work is assigning           out_put$Case <-  Inp_dat$Case         
that is

for(i in 1:4)
{
...
Case<- Inp_dat$Case[i]
?

out_put[i,]<-data.frame(Case, stdL, stdPP, stdSE, L, PP, PP_SE)

}

out_put$Case <-  Inp_dat$Case

out_put

What I don't understand is why I need to do this, and why  adding rows to
out_put[i,] within the loop the Case column has an integer label assigned
and not the text label.

Further it seems I cannot correct this within the loop?





--
View this message in context: http://r.789695.n4.nabble.com/Dataframes-and-text-identifier-columns-tp4693184p4693443.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi.

Well, Case is probably factor, which is basically numeric vector with labels. It is useful for some operations but it can have some features which lead to this behaviour. I do not have available your exact code but I presume you use c or cbind somewhere.
[1] a b c d
Levels: a b c d
[1] 1 2 3 4 1
Case
[1,]    1 1
[2,]    2 1
[3,]    3 1
[4,]    4 1

You can try to change Case to character by as.character(Case) before cycle.

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