[R] Create rows for columns in dataframe

arun smartpink111 at yahoo.com
Tue Aug 13 21:47:48 CEST 2013


You could also try:
##Out1 is the output dataset
Out1$PRIMAIRY<-as.logical(Out1$PRIMAIRY) #changing the class
#dat1 input dataset

vec1<- paste(dat1[,1],dat1[,2],colnames(dat1)[2],sep=".")
res2<-reshape(dat1,idvar="newCol",varying=list(2:26),direction="long")
res3<-res2[order(res2[,4]),]
res4<-  res3[res3[,3]!="",-4]
vec2<-paste(res4[,1],res4[,3],paste0("C",res4[,2]),sep=".")
 res4$PRIMAIRY<-vec2%in%vec1
 row.names(res4)<-1:nrow(res4)
res4$ID<- row.names(res4)
res4[,c(1,3)]<- lapply(res4[,c(1,3)],as.character)
res5<-res4[,c(5,1,3,4)]
colnames(res5)[3]<-"CODE"
identical(res5,Out1)
#[1] TRUE
A.K.



A.K.



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Tuesday, August 13, 2013 2:45 PM
Subject: Re: [R] Create rows for columns in dataframe

According to your first post,
"

NewDataFrame <- data.frame(ID=integer(), DSYSRTKY=integer(), CODE=character(),  PRIMAIRY=logical())"


The new output dataset: "Out1"
str(Out1)
'data.frame':    48 obs. of  4 variables:
 $ ID      : chr  "1" "2" "3" "4" ...
 $ DSYSRTKY: chr  "100000005" "100000005" "100000005" "100000005" ...
 $ CODE    : chr  "71535" "78900" "V1251" "V454" ...
 $ PRIMAIRY: chr  "TRUE" "FALSE" "FALSE" "FALSE" ...


I guess you wanted DSYSRTKY to be "numeric" and PRIMAIRY to be "logical"
res1<-do.call(rbind,lapply(seq_len(nrow(dat1)),function(i) {x1<-as.character(unlist(dat1[i,-1]));CODE<-x1[x1!=""];PRIMAIRY<-x1[x1!=""]==head(x1,1); DSYSRTKY=as.numeric(as.character(dat1[i,1]));data.frame(DSYSRTKY,CODE,PRIMAIRY,stringsAsFactors=FALSE) }))
 res1$ID<- row.names(res1)
res2<-res1[,c(4,1:3)]

str(res2)
#'data.frame':    48 obs. of  4 variables:
# $ ID      : chr  "1" "2" "3" "4" ...
# $ DSYSRTKY: num  1e+08 1e+08 1e+08 1e+08 1e+08 ...
# $ CODE    : chr  "71535" "78900" "V1251" "V454" ...
# $ PRIMAIRY: logi  TRUE FALSE FALSE FALSE TRUE FALSE ...
 head(res2)
#  ID  DSYSRTKY  CODE PRIMAIRY
#1  1 100000005 71535     TRUE
#2  2 100000005 78900    FALSE
#3  3 100000005 V1251    FALSE
#4  4 100000005  V454    FALSE
#5  5 100000203 45341     TRUE
#6  6 100000203  4019    FALSE
 head(Out1)
#  ID  DSYSRTKY  CODE PRIMAIRY
#1  1 100000005 71535     TRUE
#2  2 100000005 78900    FALSE
#3  3 100000005 V1251    FALSE
#4  4 100000005  V454    FALSE
#5  5 100000203 45341     TRUE
#6  6 100000203  4019    FALSE
A.K.







----- Original Message -----
From: Dark <info at software-solutions.nl>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, August 13, 2013 12:16 PM
Subject: Re: [R] Create rows for columns in dataframe

Hi,

My desired output for my sample!! using dput():
structure(list(ID = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
"31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", 
"42", "43", "44", "45", "46", "47", "48"), DSYSRTKY = c("100000005", 
"100000005", "100000005", "100000005", "100000203", "100000203", 
"100000203", "100000203", "100000315", "100000315", "100000315", 
"100000315", "100000315", "100000315", "100000315", "100000315", 
"100000315", "100000315", "100000315", "100000315", "100000315", 
"100000315", "100000315", "100000315", "100000315", "100000315", 
"100000315", "100000315", "100000327", "100000327", "100000327", 
"100000327", "100000327", "100000327", "100000327", "100000327", 
"100000327", "100000327", "100000327", "100000327", "100000327", 
"100000327", "100000327", "100000327", "100000327", "100000327", 
"100000327", "100000327"), CODE = c("71535", "78900", "V1251", 
"V454", "45341", "4019", "72400", "V1011", "42831", "5990", "8052", 
"4241", "4019", "311", "2724", "71680", "4168", "7804", "V066", 
"6930", "41400", "V4581", "40291", "4280", "5990", "V4986", "5939", 
"3109", "41401", "6826", "7850", "4019", "2720", "49390", "2859", 
"79029", "V1582", "486", "51881", "5119", "42789", "7823", "41400", 
"V4581", "40390", "5859", "49390", "2724"), PRIMAIRY = c("TRUE", 
"FALSE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "FALSE", 
"TRUE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE", 
"FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE", 
"FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "FALSE", "TRUE", 
"FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE", 
"FALSE", "TRUE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE", 
"FALSE", "FALSE", "FALSE", "FALSE", "FALSE")), .Names = c("ID", 
"DSYSRTKY", "CODE", "PRIMAIRY"), row.names = c(NA, 48L), class =
"data.frame")

So the 'DSYSRTKY' (100000005) has 4 code fields filled so you get 4 rows.
The next one also 4, the third one 16. Anyway, just take a look at the
sample.

I think this will help trying to make clear what my desired result is!

Regards Derk





--
View this message in context: http://r.789695.n4.nabble.com/Create-rows-for-columns-in-dataframe-tp4673607p4673646.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.




More information about the R-help mailing list