[R] Adding Column to a Data Frame

David L Carlson
Wed, 11 Mar 2015 17:25:07 -0000

The merge function combines 2, not 3 files at a time. Maybe

rich.stats2 = merge(rich.stats, Month, by="X.SampleID")
rich.stats3 = merge(rich.stats2, Location, by="X.SampleID")

Reading the manual page will help:

?merge

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Lauren
O'Connell
Sent: Wednesday, March 11, 2015 11:02 AM
To: r-help at r-project.org
Subject: [R] Adding Column to a Data Frame

I am trying to add a column with data to a data frame that already has
information but am getting this error:

> rich.stats4 = merge(rich.stats,Location,Month,by="X.SampleID")
Error in fix.by(by.x, x) :
  'by' must specify one or more columns as numbers, names or logical


I have two separate data frames that contain my sample names with location
and with month:

#Create data frame of all sample names and month sample was taken

Month
data.frame(X.SampleID=sample_data(Lauren5000)$X.SampleID,MonthSampleTaken=sample_data(Lauren5000)$MonthSampleTaken)

head(Month)



#Create data frame of all samples names and location of sample

Location
data.frame(X.SampleID=sample_data(Lauren5000)$X.SampleID,Location=sample_data(Lauren5000)$Location)

head(Location)

I was able to add my "MonthSampleTaken" variable by using this
command:

>rich.stats2 = merge(rich.stats, Month,by="X.SampleID")

> head(rich.stats2)

  X.SampleID    mean       sd MonthSampleTaken

1      PE101 1421.34 19.44961         February

2      PE102 1336.24 25.43882         February

3      PE104 1418.75 21.92889            March

4      PE105 1331.03 20.55712            March

5      PE107 1320.21 20.91942            March

6      PE108 1328.41 20.49247            March


I now want to add my sample site location, but can't figure out how to do
this. Any help would be greatly appreciated.


Cheers,

Lauren

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.