[R] question: data.frame data conversion

arun smartpink111 at yahoo.com
Sun Aug 4 21:43:57 CEST 2013


Hi Brijesh,
No problem.

z<- data.frame(x,y,stringsAsFactors=FALSE)  
v1<-max(with(z,tapply(x,list(x),length)))
 res<-data.frame(lapply(split(z[,-1],z$x),function(.x) {x1<-if(length(.x)!=v1) c(.x,rep(NA,v1-length(.x))) else .x;x1} ))
res
#    a    b  c
#1 1.0 1.01  2
#2 1.2 1.03  3
#3 1.1 1.00 NA


One comment about your use of as.data.frame(cbind(x,y))
z1<- as.data.frame(cbind(x,y),stringsAsFactors=FALSE)
 str(z1)
'data.frame':    8 obs. of  2 variables:
# $ x: chr  "a" "a" "a" "b" ...
# $ y: chr  "1" "1.2" "1.1" "1.01" ... #should be numeric as it gets converted during

 str(cbind(x,y)) # matrix can hold only one class.  If any of the columns are "character", all the others automatically gets converted to it.
 chr [1:8, 1:2] "a" "a" "a" "b" "b" "b" "c" "c" "1" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "x" "y"

A.K.



----- Original Message -----
From: Brijesh Gulati <brijgul at gmail.com>
To: 'arun' <smartpink111 at yahoo.com>
Cc: 'R help' <r-help at r-project.org>
Sent: Sunday, August 4, 2013 3:27 PM
Subject: RE: [R] question: data.frame data conversion

Wow... great and really compact solution. One follow-up question. How do I
solve the problem if the number of repeating values are different. For
instance, in the following "z", the data value "a" and "b" are repeating 3
times, but "c" is only repeating 2 times. Hence, I will get an error. I am
fine if I get an NA corresponding to value of the third value of "c" in the
output. Again, thanks for your help.

x = c("a","a", "a", "b","b","b", "c", "c")
  y = c(1.0, 1.2, 1.1, 1.01, 1.03, 1.0, 2.0, 3.0)
  z = as.data.frame(cbind(x,y))
> z
  x    y
1 a    1
2 a  1.2
3 a  1.1
4 b 1.01
5 b 1.03
6 b    1
7 c    2
8 c    3


-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com] 
Sent: Sunday, August 04, 2013 1:30 PM
To: Brijesh Gulati
Cc: R help
Subject: Re: [R] question: data.frame data conversion

#actually, this would be more compact

data.frame(split(z[,-1],z$x))

A.K.

----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Brijesh Gulati <brijgul at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Sunday, August 4, 2013 1:27 PM
Subject: Re: [R] question: data.frame data conversion

Hi,
May be:
z<-data.frame(x,y,stringsAsFactors=FALSE)


 simplify2array(split(z[,-1],z$x))
#       a    b
#[1,] 1.0 1.01
#[2,] 1.2 1.03
#[3,] 1.1 1.00
 as.data.frame(simplify2array(split(z[,-1],z$x)))



A.K.

----- Original Message -----
From: Brijesh Gulati <brijgul at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Sunday, August 4, 2013 8:49 AM
Subject: [R] question: data.frame data conversion

Hello, I have a data.frame with repeating rows and corresponding value. For
instance, "z" will be an example of that. 

  

x = c("a","a", "a", "b","b","b")

  y = c(1.0, 1.2, 1.1, 1.01, 1.03, 1.0)

  z = as.data.frame(cbind(x,y))



> z

  x    y

1 a    1

2 a  1.2

3 a  1.1

4 b 1.01

5 b 1.03

6 b    1



So, you see that "a" and "b" are repeated 3 times and have three different
value. I would like to convert this data into something like the following. 



   a    b
  1.0 1.01
  1.2 1.03
  1.1 1.00



In the above, repeating rows (a,b) become columns and their values show up
in their respective column. 

Finally, to clarify few things. The number of rows of each repeating item (a
or b) would be the same and hence, the number of row expected in the output
shall be the same. 




    [[alternative HTML version deleted]]

______________________________________________
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