Skip to content
Prev 343169 / 398513 Next

reshape a dataset

Great! Thanks

Sent from my LG G Flex, an AT&T 4G LTE smartphone


------ Original message------
From: Jorge I Velez
Date: Fri, Aug 15, 2014 8:54 AM
To: Sohail Khan;
Cc: Jim Lemon;R Help;
Subject:Re: [R] reshape a dataset

If that's the case, you could do the following:

d <- with(skdat, table(ID, lettertag))
d <- data.frame(cbind(ID = rownames(d), d))
rownames(d) <- NULL
d

HTH,
Jorge.-
On Fri, Aug 15, 2014 at 8:22 PM, Sohail Khan <sohail13 at gmail.com> wrote:
Thanks Jim and Jorge,
Clever solutions, the final output is a list.
How do I covert it back a dataframe?
-Sohail
On Fri, Aug 15, 2014 at 5:37 AM, Jorge I Velez <jorgeivanvelez at gmail.com> wrote:
Dear Sohail,

Using Jim's data set skdat, two more options would be

# first option
d <- with(skdat, table(ID, lettertag))
names <- colnames(d)
d <- c(list(rownames(d)), lapply(1:ncol(d), function(i) as.numeric(d[,i])))
names(d) <- c('ID', names)
d

# second option
d <- with(skdat, table(ID, lettertag))
res <- c(list(rownames(d)), sapply(apply(d, 2, list), "[", 1))
names(res)[1] <- "ID"
res

HTH,
Jorge.-
On Fri, Aug 15, 2014 at 7:19 PM, Jim Lemon <jim at bitwrit.com.au> wrote:

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