-1

I'm working with 6 stations that are currently the first column after the header column (bold) in my R console. I need to get it so that my stations/treatments are in the header column. I have done this with other data before and it's worked fine, but I get an error when working with this new csv file.

Code I'm using:

stations<-as.factor(Data$Sites)
Data2<-data.frame(Data[,-1], row.names=Data[,1]) #Data refers to my file name

Error I'm getting:

Error in data.frame(Data[, -1], row.names = Data[, 1]) :
'row.names' should specify one of the variables

Exampled of data where my code creates the error:

    # A tibble: 6 × 5
      Sites   AB1   AB2   AB3   AB4
      <chr> <int> <int> <int> <int>
    1   NS1     0     0     1     2
    2   S27     2     2     0     2
    3   S28     3     0     5     3
    4  NS29     0     6     7     0
    5  NS31     3     0     1     0
    6   S44     0     1     8     0

Please can anyone tell me either what's wrong with my existing code, or code that will do the same job.

Output of str(data):

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   6 obs. of  95 variables:

$ Sites : chr  "NS1" "S27" "S28" "NS29" ...
 $ AB1   : int  0 2 0 5 7 1
 $ AB104 : int  2 0 0 31 0 0
 $ AB105 : int  1 0 0 0 0 0
 $ AB107 : int  1 0 0 0 0 0
 $ AB110 : int  1 0 0 0 0 0
 $ AB111 : int  1 0 0 0 0 0
 $ AB113 : int  1 0 0 0 0 0
 $ AB114 : int  1 0 0 0 0 0
 $ AB115 : int  1 0 0 0 0 0
 $ AB121 : int  0 30 0 166 94 4
 $ AB128 : int  0 0 0 13 0 0
 $ AB129 : int  0 0 0 13 1 0
 $ AB131 : int  0 5 0 4 2 0
 $ AB136 : int  0 1 0 4 3 0
 $ AB137 : int  0 0 0 5 0 0
 $ AB140 : int  0 0 0 12 0 0
 $ AB141 : int  0 0 0 14 1 0
 $ AB142 : int  0 0 0 2 0 0
 $ AB143 : int  0 0 0 1 0 0
 $ AB144 : int  0 0 0 3 1 0
 $ AB145 : int  0 5 0 0 3 503
 $ AB147 : int  0 7 1 0 0 10
 $ AB148 : int  0 0 0 0 0 1
 $ AB149 : int  0 0 0 0 0 2
 $ AB150 : int  0 0 0 0 0 1
 $ AB151 : int  0 0 0 0 0 5
 $ AB152 : int  0 0 0 0 0 1
 $ AB153 : int  0 0 0 0 0 1
 $ AB154 : int  0 0 0 0 0 2
 $ AB156 : int  0 0 0 0 1 1
 $ AB16  : int  1 1 0 0 10 3
 $ AB160 : int  0 0 0 0 2 0
 $ AB164 : int  0 0 0 0 1 0
 $ AB169 : int  2 0 0 31 0 0
 $ AB17  : int  1 154 33 14388 1018 158
 $ AB170 : int  0 0 0 0 0 1
 $ AB171 : int  0 0 0 82 1 6
 $ AB172 : int  0 4 0 2 0 0
 $ AB174 : int  0 7 0 0 0 0
 $ AB175 : int  0 1 0 0 0 0
 $ AB18  : int  0 2 6 0 0 2
 $ AB19  : int  3 54 83 0 25 18
 $ AB2   : int  0 19 2 8 6 5
 $ AB20  : int  0 0 1 0 3 2
 $ AB21  : int  0 24 1 3 41 4
 $ AB228 : int  0 1 0 0 0 0
 $ AB24  : int  0 0 0 1 0 0
 $ AB26  : int  0 3 11 2 1 0
 $ AB27  : int  0 39 17 8 6 28
 $ AB28  : int  2 49 41 1 94 43
 $ AB30  : int  47 89 100 24 32 20
 $ AB31  : int  0 3 2 1 1 2
 $ AB32  : int  2 4 8 19 4 1
 $ AB36  : int  0 0 1 1 0 1
 $ AB38  : int  6 0 0 1 2 1
 $ AB4   : int  0 0 0 0 1 0
 $ AB41  : int  0 4 2 0 0 6
 $ AB42  : int  1 1 0 2 0 0
 $ AB43  : int  0 106 75 161 274 109
 $ AB45  : int  0 0 0 1 4 2
 $ AB48  : int  0 13 8 0 5 22
 $ AB53  : int  0 0 14 0 0 0
 $ AB54  : int  0 0 2 0 0 2
 $ AB56  : int  0 0 0 1 0 1
 $ AB57  : int  0 0 0 0 1 0
 $ AB59  : int  0 1 1 1 0 0
 $ AB60  : int  0 3 0 0 0 0
 $ AB64  : int  0 0 1 0 0 0
 $ AB65  : int  0 0 1 0 0 0
 $ AB68  : int  0 0 1 0 0 0
 $ AB69  : int  0 0 3 0 0 0
 $ AB7   : int  4 9 2 21 16 6
 $ AB70  : int  0 0 1 0 0 0
 $ AB71  : int  0 0 7 0 0 0
 $ AB72  : int  0 0 1 0 0 0
 $ AB73  : int  0 0 2 0 0 0
 $ AB74  : int  0 2 1 0 0 5
 $ AB75  : int  0 0 1 0 0 0
 $ AB76  : int  0 0 3 0 0 0
 $ AB77  : int  16 0 2 0 2 3
 $ AB78  : int  0 0 2 0 0 1
 $ AB79  : int  0 0 8 0 0 0
 $ AB80  : int  0 0 1 0 0 0
 $ AB85  : int  0 0 1 0 0 4
 $ AB86  : int  0 5 1 6 0 0
 $ AB88  : int  0 0 1 0 1 0
 $ AB90  : int  0 0 1 0 0 0
 $ AB91  : int  0 0 1 0 0 0
 $ AB92  : int  0 0 1 0 0 3
 $ AB93  : int  0 0 1 0 0 0
 $ AB98  : int  15 0 0 0 0 0
 $ OTU231: int  2 37 114 3 9 55
 $ OTU339: int  0 5 2 1 3 10
 $ OTU347: int  0 499 123 0 0 14
 - attr(*, "spec")=List of 2
  ..$ cols   :List of 95
  .. ..$ Sites : list()
  .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
  .. ..$ AB1   : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB104 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB105 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB107 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB110 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB111 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB113 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB114 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB115 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB121 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB128 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB129 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB131 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB136 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB137 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB140 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB141 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB142 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB143 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB144 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB145 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB147 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB148 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB149 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB150 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB151 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB152 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB153 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB154 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB156 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB16  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB160 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB164 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB169 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB17  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB170 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB171 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB172 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB174 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB175 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB18  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB19  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB2   : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB20  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB21  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB228 : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB24  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB26  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB27  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB28  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB30  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB31  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB32  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB36  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB38  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB4   : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB41  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB42  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB43  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB45  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB48  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB53  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB54  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB56  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB57  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB59  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB60  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB64  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB65  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB68  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB69  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB7   : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB70  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB71  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB72  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB73  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB74  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB75  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB76  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB77  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB78  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB79  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB80  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB85  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB86  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB88  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB90  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB91  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB92  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB93  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ AB98  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ OTU231: list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ OTU339: list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ OTU347: list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  ..$ default: list()
  .. ..- attr(*, "class")= chr  "collector_guess" "collector"
  ..- attr(*, "class")= chr "col_spec"

Output of str(data[,1]): chr [1:6] "NS1" "S27" "S28" "NS29" "NS31" "S44"

  • This worked for me on a test data.frame. Please share an example data.frame where this doesn't work for you. Take a look at this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – lmo Feb 22 '17 at 15:20
  • @lmo - I am unsure of how to do this - very new to coding and stackoverflow. – Amelia Bridges Feb 22 '17 at 15:50
  • You best bet is with `dput`. Copy the results of something like `dput(df)` into your question. Please see the link in my comment above for more details. – lmo Feb 22 '17 at 15:52
  • I think that helps, I copied in the results of head(Data) to show a section of my matrix. I need the Sites column to shift one to the left. – Amelia Bridges Feb 22 '17 at 16:03
  • Ok. Good start. Now you need to produce an example that replicates the error. When I read this in as a data.frame, and perform the "shifting" operation, I don't get an error message. – lmo Feb 22 '17 at 16:08
  • When you say 'read this in', do you mean import as a csv/txt file? When I run the code listed above, I get the error listed above. – Amelia Bridges Feb 22 '17 at 16:12

1 Answers1

0

To prevent smashing stuff into the comments, the code

df <- read.table(header=TRUE, text="    Sites   AB1   AB2   AB3   AB4
1   NS1     0     0     1     2
2   S27     2     2     0     2
3   S28     3     0     5     3
4  NS29     0     6     7     0
5  NS31     3     0     1     0
6   S44     0     1     8     0")

df2 <- data.frame(df[,-1], row.names=df[,1])

runs without error and produces

df2
     AB1 AB2 AB3 AB4
NS1    0   0   1   2
S27    2   2   0   2
S28    3   0   5   3
NS29   0   6   7   0
NS31   3   0   1   0
S44    0   1   8   0

From your output, it looks like this should work. You might try being super-explicit:

df2 <- data.frame(as.data.frame(df[,-1]), row.names=as.character(df[,1]))

replacing df with the name of your data.frame as discussed in the comments.

Alternatively, in two lines

df2 <- as.data.frame(df[,-1])
row.names(df) <- as.character(df[,1])
lmo
  • 37,904
  • 9
  • 56
  • 69
  • Thanks, do you mind if I ask what computer system you're running this one and what version of R? The only thing I can think of that's making a difference is possibly the fact i'm working on Mac; I'm using the newest edition of R. – Amelia Bridges Feb 22 '17 at 16:58
  • Thank you. Okay, running system shouldn't make a difference then. The code above gives my another error: Error in nofish[, 1] : object of type 'closure' is not subsettable – Amelia Bridges Feb 23 '17 at 12:02
  • My csv is 'nofish'. If I run 'nofish2<-data.frame(df[,-1], row.names=df[,1])' I also get 'Error in df[, 1] : object of type 'closure' is not subsettable'. I thought the problem would be to do with the 1 and -1, but when I change these and run the MDS, it doesn't plot the correct thing. Sorry if the questions/understanding seems basic, I'm quite new to R and have only really studied basic non-/parametric tests. I appreciate your help massively! – Amelia Bridges Feb 23 '17 at 13:50
  • df is often used as a generic name for a data.frame. Unfortunately, in this case, it is also a function that calculates the cumulative density of the F distribution, which is the cause of the error message. You would replace df with nofish: `nofish2 <- data.frame(nofish[, -1], row.names=nofish[,1])`. – lmo Feb 23 '17 at 14:33
  • I see, thanks for explaining that. That was the exact code I was using originally `nofish2<-data.frame(nofish[,-1], row.names=df[,1])`, but it's still giving the error: `Error in data.frame(nofish[, -1], row.names = nofish[, 1]) : 'row.names' should specify one of the variables`. I know the code works as I've used it on other data but it doesn't seem to like this set. – Amelia Bridges Feb 23 '17 at 16:26
  • I have posted the output above. It's rather long though as I have 94 variable. It's saying 95 because it's still counting 'Sites' as a variable - this is the problem. – Amelia Bridges Feb 23 '17 at 16:42
  • OK. Below that post the output of `str(nofish[,1])`. – lmo Feb 23 '17 at 16:53
  • Posted the output above. – Amelia Bridges Feb 23 '17 at 16:58