0

I am currently trying to create a data frame from another data frame one row at a time. I am trying to rename the last row (as soon as it is added to the data frame) to a given value (stored in another list as entry[1]).

Currently, I am using

a <- rbind(a, choice)
rownames(a)[nrow(a)] <- entry[1] 

However, my output for this still has the rows labeled as a numeric list

  optOne optTwo Neither
1  0     1     0
2  1     0      0
n 'count''count''count'

as opposed to

         optOne  optTwo  Neither
 cac1001  0      1       0
 f253     1      0       1
 'name'  'count' 'count' 'count'

I considered saving the name column as part of the actual data in its own column, but this was untenable because I need to be able to perform arithmetic on the actual data in my dataframe

EDIT: Current output for rbind(a, choice) and relevant entryList[1] for the first 10 entries (I will list entryList[1] as an additional column):

     optOne    optTwo    Neither    entryList[1]
 1   1         0         0          cac1002
 2   1         0         0          cac101
 3   1         0         0          cac102
 4   1         0         0          cac1101
 5   1         0         0          cac1102
 6   1         0         0          cac201
 7   1         0         0          cac202
 8   0         1         0          cac301
 9   0         0         1          cac302
10   1         0         0          cac402   

EDIT2: One thing that seems rather odd: parts of my code later on that rely on an if loop

 if(entry[1] %in% rownames(a)) {

and they work correctly as if entry[1] was being added to rownames properly

guildenstern
  • 15
  • 2
  • 5
  • Gonna need a little more info please. Like the data and variables. And also, why don't you just add them all at the end? – Rich Scriven Jun 23 '14 at 18:24
  • Data and variables added, though I'm not entirely certain how useful they'll be (also, my full set of data is enormous and not worth reposting, I have copied only the first few lines and then an example of the format). As for changing all the names at the end, I'm using the dataframe as a hashmap--there are multiple entries in my data that have the same `entry[1]` name and I want to combine them to get a total count. My code will ideally search if the dataframe already contains a name and if so, increment the counts on that entry as opposed to creating a new one. – guildenstern Jun 23 '14 at 18:50
  • Can I have the result of `a <- rbind(a, choice)` and the value of `entry[1]`? – Rich Scriven Jun 23 '14 at 18:51
  • see if `a <- rbind(a, choice,deparse.level=1)`. I will shortly incorporate `entry[1]` in this setup – Silence Dogood Jun 23 '14 at 18:54
  • @RichardScriven, I have updated the original post with those values. – guildenstern Jun 23 '14 at 19:04
  • @Osssan, that didn't seem to change anything (output was identical). – guildenstern Jun 23 '14 at 19:05

1 Answers1

0

With your sample data, let us know if this works

#Your data
df<-read.table(text="optOne  ,  optTwo  , Neither  , entryList
   1    ,     0    ,    0     ,    cac1002
   1    ,     0    ,    0     ,    cac101
   1    ,     0    ,    0     ,    cac102
   1    ,     0    ,    0     ,    cac1101
   1    ,     0    ,    0     ,    cac1102
   1    ,     0    ,    0     ,    cac201
   1    ,     0    ,    0     ,    cac202
   0    ,     1    ,    0     ,    cac301
   0    ,     0    ,    1     ,    cac302
   1    ,     0    ,    0     ,    cac402",sep=",",header=TRUE,stringsAsFactors=FALSE)
head(df)  
#     > head(df)  
#  optOne optTwo Neither   entryList
#1      1      0       0     cac1002
#2      1      0       0      cac101
#3      1      0       0      cac102
#4      1      0       0     cac1101
#5      1      0       0     cac1102
#6      1      0       0      cac201

#Create first element
entryList<-as.list(df$entryList)
a=df[1,]
row.names(a)<-entryList[[1]]

#> a
#     optOne optTwo Neither   entryList
#cac1002      1      0       0     cac1002

new.df<-do.call(rbind, lapply(1:nrow(df),function(x) { 
  if(x>1) { 
  z=df[x,];
  row.names(z)[nrow(z)]<-entryList[[x]];
  } 
  else { z=a;
  } 
  return(z) 
  } 
  ))
#> head(new.df)
#     optOne optTwo Neither   entryList
#cac1002      1      0       0     cac1002
#cac101       1      0       0      cac101
#cac102       1      0       0      cac102
#cac1101      1      0       0     cac1101
#cac1102      1      0       0     cac1102
#cac201       1      0       0      cac201

#Check Equality
setdiff(df,new.df)
#> setdiff(df,new.df)
#data frame with 0 columns and 0 rows

Previous:

I have assumed sample data, see if this works for you:

entry='d'
set.seed(100)
a=rnorm(5)
b=rnorm(5)
c=rbind(a,b,deparse.level=2)

#> c
#        [,1]       [,2]        [,3]       [,4]       [,5]
#a -0.5021924  0.1315312 -0.07891709  0.8867848  0.1169713
#b  0.3186301 -0.5817907  0.71453271 -0.8252594 -0.3598621

row.names(c)[nrow(c)]<-entry

#> c
#        [,1]       [,2]        [,3]       [,4]       [,5]
#a -0.5021924  0.1315312 -0.07891709  0.8867848  0.1169713
#d  0.3186301 -0.5817907  0.71453271 -0.8252594 -0.3598621
Silence Dogood
  • 3,587
  • 1
  • 13
  • 17
  • Nope, that doesn't seem to change anything. I think the issue may be that my `entry` is a list with multiple values? But I don't see why it can't use the value stored in `entry[1]` when I call it. – guildenstern Jun 23 '14 at 19:51
  • if `entry` is a `list`, you should access elements with `entry[[1]]`, see this (http://stackoverflow.com/questions/1169456/in-r-what-is-the-difference-between-the-and-notations-for-accessing-the) – Silence Dogood Jun 23 '14 at 20:06
  • That does not appear to make a difference. – guildenstern Jun 23 '14 at 20:07