ID column corrupting data frame

I am working with the R programming language. Suppose I have the following data frame:

a = rnorm(100,10,1)
b = rnorm(100,10,5)
c = rnorm(100,10,10)

my_data_2 = data.frame(a,b,c)

my_data_2$group = as.factor(C)

My Question: Suppose I want to add an ID column to this data frame that ranks the first observation as "100" and increases the ID by 1 for each new column. I tried to do this as follows:

my_data_2$id = seq(101, 200, by = 1)

However, this "corrupted" the data frame:

head(my_data_2)
          a         b          c
1 10.381397  9.534634 12.8330946
2 10.326785  6.397006  8.1217063
3  8.333354 11.474064 11.6035562
4  9.583789 12.096404 18.2764387
5  9.581740 12.302016  4.0601871
6 11.772943  9.151642 -0.3686874
                                                                                                                                                                                                                                                                                                                                        group
1        c(9.98552413605153, 9.53807731118048, 6.92589246998173, 8.97095368638206, 9.70249918748529, 10.6161773148626, 9.2514231659343, 10.6566757899233, 10.2351848084123, 9.45970725813352, 9.15347719257448, 9.30428244749624, 8.43075784609759, 11.1200169905262, 11.3493313166827, 8.86895968334901, 9.13208319045466, 9.70062759133717)
2          c(8.90358954387628, 13.8756093430144, 12.9970566311467, 10.4227745183785, 21.3259516051226, 4.88590162247496, 10.260282181, 14.092109840631, 7.37839577680487, 9.09764173775965, 15.1636139760987, 9.9773055885761, 8.29361737323061, 8.61361852648607, 12.6807897406641, 0.00863359720839085, 10.7660528147358, 9.79616528370632)
3 c(25.8063583646201, -11.5722310383483, 8.56096791164312, 12.2858029391835, -0.312392781809937, 0.946343715084028, 2.45881422753051, 7.26197515743391, 0.333766891336273, 14.9149659649045, -4.55483090530928, -19.8075232688082, 16.59106194569, 18.7377329188129, 1.1771203751127, -6.19019973790205, -5.02277721344565, 23.3363430334739)
4                                                                                                                                                                                                                                                                                     c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)
5                                                                                                                                                                                                                                                 c("B", "B", "B", "A", "B", "B", "B", "B", "B", "B", "B", "A", "B", "B", "B", "B", "B", "B")
6                                                                                                                                                                                                                                                 c("B", "B", "B", "B", "B", "A", "B", "B", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B")
   id
1 101
2 102
3 103
4 104
5 105
6 106

Can someone please show me how to fix this problem?

Thanks!

The problem is likely that you have something defined as C in your working environment which you are not showing.

You are calling a function C uppercase not c, lowercase in your data.frame
Try this to see what is happening.

?C

I think you want

my_data_2$group = as.factor(my_data_2$c)

That does not affect the problem with the output though. There appears to be a C defined in the working environment.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.