Hi:
This isn't the most elegant way, I'm sure, but here's one approach.
library(reshape)
# read in your data...I had to surround the text strings with quotes because
of the spaces
df <- read.table(textConnection("
TractID StandID Species CruiseDate DBHClass TreesPerAcre
'Carbon Stand' 1 'Loblolly Pine' 5/20/2010 10
1.2
'Carbon Stand' 1 'Loblolly Pine' 5/20/2010 12
1.2
'Carbon Stand' 1 'Loblolly Pine' 5/20/2010 14
0.61
'Carbon Stand' 1 'Loblolly Pine' 5/20/2010 16
0.75
'Carbon Stand' 1 'Hard Hardwoods' 5/20/2010 6
27.4
'Carbon Stand' 1 'Hard Hardwoods' 5/20/2010 8 16.4
'Carbon Stand' 1 'Hard Hardwoods' 5/20/2010 10 16.1
'Carbon Stand' 1 'Hard Hardwoods' 5/20/2010 12 18.3
'Carbon Stand' 1 'Hard Hardwoods' 5/20/2010 14
9.9"), header TRUE)
# Generate a matrix that expands the DBHs in each row and column concatenate
DBH <- t(apply(df, 1, function(x) as.numeric(x[5]) + seq(-0.9, 1, by 0.1)))
df2 <- cbind(df, DBH)
# Divide the TreesPerAcre by 20
df2$TreesPerAcre <- df2$TreesPerAcre/20
# reshape from wide to long using the reshape function melt()
df3 <- melt(df2, id = 1:6) # a 180 x 8 data frame
df3 <- df3[order(df3$DBHClass, df3$value), ]> head(df3)
TractID StandID Species CruiseDate DBHClass TreesPerAcre
5 Carbon Stand 1 Hard Hardwoods 5/20/2010 6 1.37
14 Carbon Stand 1 Hard Hardwoods 5/20/2010 6 1.37
23 Carbon Stand 1 Hard Hardwoods 5/20/2010 6 1.37
32 Carbon Stand 1 Hard Hardwoods 5/20/2010 6 1.37
41 Carbon Stand 1 Hard Hardwoods 5/20/2010 6 1.37
50 Carbon Stand 1 Hard Hardwoods 5/20/2010 6 1.37
variable value
5 1 5.1
14 2 5.2
23 3 5.3
32 4 5.4
41 5 5.5
50 6 5.6
You'll likely need to convert the CruiseDate to a date variable at some
point; see ?as.Date, especially the format argument.
If you need this as a function to be applied multiple times,
DBHspread <- function(df) {
# Function to spread out the DBHClass values over an interval of
# length 2 in increments of 0.1, and divide TreesPerAcre by 20 to
# compensate for change in class width.
require(reshape)
# Generate a matrix that expands the DBHs in each row and column
concatenate
# Since DBHClass (variable 5) is integer, it needs to be converted to
numeric
# In this context, x is the i-th row of the data frame.
DBH <- t(apply(df, 1, function(x) as.numeric(x[5]) + seq(-0.9, 1, by
0.1)))
# Concatenate new DBHClass values to existing data frame
df2 <- cbind(df, DBH)
# Divide the TreesPerAcre by 20
df2$TreesPerAcre <- df2$TreesPerAcre/20
# reshape from wide to long using the reshape function melt()
df3 <- melt(df2, id = 1:6)
# Sort by DBHClass and value within it, output
with(df3, df3[order(DBHClass, value), ])
}
HTH,
Dennis
On Wed, Sep 15, 2010 at 10:17 AM, Randy Cass
<randynewruser@gmail.com>wrote:
> R Users,
>
> I am new to R and have tried to figure out how to automate this
> process instead of using excel. I have read in this dataframe into r
> with read.table. I need to reshape the data from the first table into
> the format of the second table.
>
> TractID StandID Species CruiseDate DBHClass TreesPerAcre
> Carbon Stand 1 Loblolly Pine 5/20/2010 10 1.2
> Carbon Stand 1 Loblolly Pine 5/20/2010 12 1.2
> Carbon Stand 1 Loblolly Pine 5/20/2010 14 0.61
> Carbon Stand 1 Loblolly Pine 5/20/2010 16 0.75
> Carbon Stand 1 Hard Hardwoods 5/20/2010 6 27.4
> Carbon Stand 1 Hard Hardwoods 5/20/2010 8 16.4
> Carbon Stand 1 Hard Hardwoods 5/20/2010 10 16.1
> Carbon Stand 1 Hard Hardwoods 5/20/2010 12 18.3
> Carbon Stand 1 Hard Hardwoods 5/20/2010 14 9.9
>
> I want the TreesPerAcre number to be divided evenly (1.2/20) over the
> tenth inch DBHClasses. For example, A 10 inch DBHClass would range
> from 9.1 to 11.0, the 12 inch class would be from 11.1 to 13.0, etc.
> Any help will be greatly appreciated.
>
> TractID StandID Species CruiseDate DBHClass TreesPerAcre
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.1 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.2 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.3 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.4 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.5 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.6 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.7 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.8 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 9.9 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.0 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.1 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.2 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.3 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.4 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.5 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.6 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.7 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.8 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 10.9 0.06
> Carbon Stand 1 Loblolly Pine 5/20/2010 11.0 0.06
> etc.
>
> Information:
> R 2.11.1
> Windows 7
>
> Thanks in advance,
>
> Randy
>
> ______________________________________________
> R-help@r-project.org mailing list
> 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.
>
[[alternative HTML version deleted]]