Skip to content

Data alignment

5 messages · Sarah Goslee, jim holtman, syrvn +1 more

#
Hello!

I have a data.frame which looks like:

Name - Value
A - 400
A - 300
B - 200
B - 350
C - 500
C - 350
D - 450 
D - 600
E - 700 
E - 750
F - 630
F - 650

I want to add another column where all A,B should get an index 1, all C,D an
index of 2 and all E,F an index of 3 so that the data.frame looks like:

ID - Name - Value
1 - A - 400
1 - A - 300
1 - B - 200
1 - B - 350
2 - C - 500
2 - C - 350
2 - D - 450 
2 - D - 600
3 - E - 700 
3 - E - 750
3 - F - 630
3 - F - 650

My data.frame is quite big so I cannot add all values by hand.


Cheers




--
View this message in context: http://r.789695.n4.nabble.com/Data-alignment-tp4153024p4153024.html
Sent from the R help mailing list archive at Nabble.com.
#
try this:
+ A - 400
+ A - 300
+ B - 200
+ B - 350
+ C - 500
+ C - 350
+ D - 450
+ D - 600
+ E - 700
+ E - 750
+ F - 630
+ F - 650", header = TRUE, as.is = TRUE)
+                 , value = c( 1,   1,   2,   2,   3,   3)
+                 , stringsAsFactors = FALSE
+                 )
Name X. Value ID
1     A  -   400  1
2     A  -   300  1
3     B  -   200  1
4     B  -   350  1
5     C  -   500  2
6     C  -   350  2
7     D  -   450  2
8     D  -   600  2
9     E  -   700  3
10    E  -   750  3
11    F  -   630  3
12    F  -   650  3
On Sat, Dec 3, 2011 at 7:06 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:

  
    
#
Thanks for your suggestions. I will try them.

The "-" in my original post was actually only there to serve as a separator
so that it is easier for you to see the data structure but apparently it
rather confused you... sorry :)

--
View this message in context: http://r.789695.n4.nabble.com/Data-alignment-tp4153024p4153112.html
Sent from the R help mailing list archive at Nabble.com.
1 day later
#
separator
That is why dput is to be used. Try this by yourself
structure(c(2L, 1L, 1L, NA, 2L, 1L, NA, NA, 2L, NA, NA, NA, NA, 
1L, NA, NA, NA, 2L, NA, 1L, NA, 2L, NA, 3L, NA), .Dim = c(5L, 
5L), .Dimnames = list(NULL, c("", "BMW", "Mercedes", "VW", "Skoda"
)))

x<-structure(c(2L, 1L, 1L, NA, 2L, 1L, NA, NA, 2L, NA, NA, NA, NA, 
1L, NA, NA, NA, 2L, NA, 1L, NA, 2L, NA, 3L, NA), .Dim = c(5L, 
5L), .Dimnames = list(NULL, c("", "BMW", "Mercedes", "VW", "Skoda"
)))
[1] TRUE
Regards
Petr
http://www.R-project.org/posting-guide.html