0

I'm in a very basic class that introduces R for genetic purposes. I'm encountering a rather peculiar problem in trying to follow the instructions given. Here is what I have along with the instructor's notes:

MangrovesRaw<-read.csv("C:/Users/esteb/Documents/PopGen/MangrovesSites.csv")
#i'm going to make a new dataframe now, with one column more than the mangrovesraw dataframe but the same number of rows. 
View(MangrovesRaw)
Mangroves<-data.frame(matrix(nrow = 528, ncol = 23))

#next I want you to name the first column of Mangroves "pop"

colnames(Mangroves)<-c(col1="pop")

#i'm now assigning all values of that column to be 1
Mangroves$pop<-1
#assign the rest of the columns (2 to 23) to the entirety of the MangrovesRaw dataframe

#then change the names to match the mangroves raw names
colnames(Mangroves)[2:23]<-colnames(MangrovesRaw)

I'm not really sure how to assign columns that haven't been named used the $ as we have in the past. A friend suggested I first run

colnames(Mangroves)[2:23]<-colnames(MangrovesRaw)
Mangroves$X338<-MangrovesRaw
#X338 is the name of the first column from MangrovesRaw

But while this does transfer the data from MangrovesRaw, it comes at the cost of having my column names messed up with X338. added to every subsequent column. In an attempt to modify this I found the following "fix"

colnames(Mangroves)[2:23]<-colnames(MangrovesRaw)
Mangroves$X338<-MangrovesRaw[,2]
#Mangroves$X338<-MangrovesRaw[,2:22]
#MangrovesRaw has 22 columns in total

While this transferred all the data I needed for the X338 Column, it didn't transfer any data for the remaining 21 columns. The code in # just results in the same problem of having X388. show up in all my column names.

What am I doing wrong?

Anon
  • 3
  • 2
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 21 '20 at 00:19

1 Answers1

0

There are a few ways to solve this problem. It may be that your instructor wants it done a certain way, but here's one simple solution: just cbind() the Mangroves$pop column with the real data. Then the data and column names are already added.

Mangroves <- cbind(Mangroves$pop, MangrovesRaw)

Here's another way:

Mangroves[, 2:23] <- MangrovesRaw
colnames(Mangroves)[2:23] <- colnames(MangrovesRaw)
andrew_reece
  • 20,390
  • 3
  • 33
  • 58
  • Thank you so, so much! This definitely worked. My instructor is pretty lax about how we run our code but we definitely never learned about cbind(). What are some other, simple ways that come to mind to do this? I'm not sure what was wrong with my code that it kept messing up the column titles. – Anon Nov 21 '20 at 00:45
  • You're welcome. I added another way - it's not as simple as just smushing the two things together with `cbind` but may be more like what your prof had in mind. – andrew_reece Nov 21 '20 at 01:32
  • 1
    Ah yes the second one you posted is exactly what we were expected to do and now that I think about it, that makes a lot of sense. Thanks for the wonderful answers – Anon Nov 21 '20 at 02:10