Create function to fit multiple models to 1 dataset - r

I am trying to fit a GLM to a small dataset, consisting of 5 columns of variables y, x1, x2, x3, x4, and 24 rows of data.
This is not a problem in itself, but with these predictive variables there are 2^4 models possible. I am trying to write a function such that it will create a GLM for all different models, and return the coefficients along with the AIC value in 1 table. Can anyone help me out?
The dataset looks like this:
i y x1 x2 x5 x7
1 29.5 5.0208 1.0 2.0 4
2 27.9 4.5429 1.0 1.0 3
3 25.9 4.5573 1.0 1.0 3
4 29.9 5.0597 1.0 1.0 3
5 29.9 3.8910 1.0 1.0 3
6 30.9 5.8980 1.0 1.0 3
7 28.9 5.6039 1.0 0.0 3
8 35.9 5.8282 1.0 2.0 3
9 31.5 5.3003 1.0 1.0 3
10 31.0 6.2712 1.0 1.0 2
11 30.9 5.9592 1.0 2.0 3
12 30.0 5.0500 1.0 0.0 2
13 36.9 8.2464 1.5 2.0 4
14 41.9 6.6969 1.5 1.5 3
15 40.5 7.7841 1.5 1.0 3
16 43.9 9.0384 1.0 1.5 3
17 37.5 5.9894 1.0 2.0 3
18 37.9 7.5422 1.5 1.0 3
19 44.5 8.7951 1.5 2.0 4
20 37.9 6.0831 1.5 1.0 3
21 38.9 8.3607 1.5 2.0 4
22 36.9 8.1400 1.0 2.0 3
23 45.8 9.1416 1.5 1.5 4
24 25.9 4.9176 1.0 1.0 4
And the dput is:
structure(list(y = c(29.5, 27.9, 25.9, 29.9, 29.9, 30.9, 28.9,
35.9, 31.5, 31, 30.9, 30, 36.9, 41.9, 40.5, 43.9, 37.5, 37.9,
44.5, 37.9, 38.9, 36.9, 45.8, 25.9), x1 = c(5.0208, 4.5429, 4.5573,
5.0597, 3.891, 5.898, 5.6039, 5.8282, 5.3003, 6.2712, 5.9592,
5.05, 8.2464, 6.6969, 7.7841, 9.0384, 5.9894, 7.5422, 8.7951,
6.0831, 8.3607, 8.14, 9.1416, 4.9176), x2 = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1, 1, 1.5, 1.5, 1.5, 1.5,
1, 1.5, 1), x5 = c(2, 1, 1, 1, 1, 1, 0, 2, 1, 1, 2, 0, 2, 1.5,
1, 1.5, 2, 1, 2, 1, 2, 2, 1.5, 1), x7 = c(4, 3, 3, 3, 3, 3, 3,
3, 3, 2, 3, 2, 4, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 4)), .Names = c("y",
"x1", "x2", "x5", "x7"), row.names = c("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "23", "24"), class = "data.frame")

I used the glmulti package (link below) a few years ago to do something similar to this. I don't know if it will work for your problem though (you should post your data or a subset of your data so people can try things out).
https://www.jstatsoft.org/article/view/v034i12/v34i12.pdf

Related

Use numbers as column names while grouping them

Here is my sample data;
mydata<-structure(list(x1 = c(0, 8.6, 11.2, 8.4, 0, 0), x2 = c(0, 0,
7.8, 7.6, 1.2, 10.2), y1 = c(0, 0, 3.4, 21.4, 1.8, 1.4), y2 = c(7.8,
7.6, 1.2, 10.2, 7, 0), z1 = c(0, 1.6, 7.6, 23.6, 3.2, 0), z2 = c(8.6,
1.4, 0, 0, 0, 0)), .Names = c("x1", "x2", "y1", "y2", "z1", "z2"
), class = "data.frame", row.names = c(NA, -6L))
x1 x2 y1 y2 z1 z2
1 0.0 0.0 0.0 7.8 0.0 8.6
2 8.6 0.0 0.0 7.6 1.6 1.4
3 11.2 7.8 3.4 1.2 7.6 0.0
4 8.4 7.6 21.4 10.2 23.6 0.0
5 0.0 1.2 1.8 7.0 3.2 0.0
6 0.0 10.2 1.4 0.0 0.0 0.0
With the code below, it is possible to group columns as x, y and z.
grps <- unique(gsub("[0-9]", "", colnames(mydata)))
# [1] "x" "y" "z"
But When I rename columns like that;
myd<-structure(list(X2005 = c(0, 8.6, 11.2, 8.4, 0, 0), X2005.1 = c(0,
0, 7.8, 7.6, 1.2, 10.2), X2006 = c(0, 0, 3.4, 21.4, 1.8, 1.4),
X2006.1 = c(7.8, 7.6, 1.2, 10.2, 7, 0), X2007 = c(0, 1.6,
7.6, 23.6, 3.2, 0), X2007.1 = c(8.6, 1.4, 0, 0, 0, 0)), .Names = c("X2005",
"X2005.1", "X2006", "X2006.1", "X2007", "X2007.1"), row.names = c(NA,
6L), class = "data.frame")
X2005 X2005.1 X2006 X2006.1 X2007 X2007.1
1 0.0 0.0 0.0 7.8 0.0 8.6
2 8.6 0.0 0.0 7.6 1.6 1.4
3 11.2 7.8 3.4 1.2 7.6 0.0
4 8.4 7.6 21.4 10.2 23.6 0.0
5 0.0 1.2 1.8 7.0 3.2 0.0
6 0.0 10.2 1.4 0.0 0.0 0.0
I want to see;
# [1] "2005" "2006" "2007"
We can use gsub to match the letter 'X' at the beginning (^) of the string or (| the . followed by numbers at the end ($) of the string and replace with blank ("")
names(myd) <- gsub("^X|\\.\\d+$", "", names(myd))
names(myd)
#[1] "2005" "2005" "2006" "2006" "2007" "2007"
unique(names(myd))
#[1] "2005" "2006" "2007"
If we know the number of digits and position, then substr would be faster
substr(names(myd), 2, 5)
One option would be to to use sub and convert the names to factor with labels as needed.
names(mydata) <- factor(sub("[0-9]", "", names(mydata)), labels = 2005:2007)
and then check your column names
names(mydata)
#[1] "2005" "2005" "2006" "2006" "2007" "2007"

R - Match values from multiple columns in a data.frame to a lookup table

Goal:
I want to turn the values in t1 and t2 from df data into some other value (PWT) that is in a lookup table lookup. I've seen some tutorials on how to do this for a single column, but I want to do it programmatically for an indefinite number of t columns (e.g. t1, t2, t3, t4, t5, ...) from data.
Lookup
# A tibble: 6 x 4 (HEAD)
Response `Final Fil.` Adjustment PWT
<chr> <dbl> <dbl> <dbl>
1 00000 9.00 0.500 9.50
2 00001 9.00 -0.500 8.50
3 00010 7.00 0.500 7.50
4 00011 7.00 -0.500 6.50
5 00100 7.00 0.500 7.50
6 00101 7.00 -0.500 6.50
lookup w/ dput
structure(list(Response = c("00000", "00001", "00010", "00011",
"00100", "00101", "00110", "00111", "01000", "01001", "01010",
"01011", "01100", "01101", "01110", "01111", "10000", "10001",
"10010", "10011", "10100", "10101", "10110", "10111", "11000",
"11001", "11010", "11011", "11100", "11101", "11110", "1111"),
`Final Fil.` = c(9, 9, 7, 7, 7, 7, 5, 5, 7, 7, 5, 5, 5, 5,
3, 3, 7, 7, 5, 5, 5, 5, 3, 3, 5, 5, 3, 3, 3, 3, 1, 2), Adjustment = c(0.5,
-0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5,
0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5,
-0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5), PWT = c(9.5,
8.5, 7.5, 6.5, 7.5, 6.5, 5.5, 4.5, 7.5, 6.5, 5.5, 4.5, 5.5,
4.5, 3.5, 2.5, 7.5, 6.5, 5.5, 4.5, 5.5, 4.5, 3.5, 2.5, 5.5,
4.5, 3.5, 2.5, 3.5, 2.5, 1.5, 1.5)), .Names = c("Response",
"Final Fil.", "Adjustment", "PWT"), row.names = c(NA, -32L), class = c("tbl_df",
"tbl", "data.frame"))
Data
# A tibble: 6 x 4 (HEAD)
Mouse Group t1 t2
<dbl> <chr> <chr> <chr>
1 1.00 SNI 00011 00000
2 2.00 Sham 00011 00001
3 3.00 SNI 00000 00001
4 4.00 Sham 00110 00000
5 5.00 SNI 00001 00001
6 6.00 Sham 00010 00101
data w/ dput
structure(list(Mouse = c(1, 2, 3, 4, 5, 6, 7, 8), Group = c("SNI",
"Sham", "SNI", "Sham", "SNI", "Sham", "SNI", "Sham"), t1 = c("00011",
"00011", "00000", "00110", "00001", "00010", "01001", "00110"
), t2 = c("00000", "00001", "00001", "00000", "00001", "00101",
"00100", "00010")), .Names = c("Mouse", "Group", "t1", "t2"), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
I was able to do this for t1 in data using this code:
indices <- (match(x = data$t1, table = lookup$Response))
response <- mutate(data, t1=lookup$PWT[indices])
Output in new table response
Mouse Group t1 t2 t3 t4 t5 t6
<dbl> <chr> <dbl> <chr> <lgl> <lgl> <lgl> <lgl>
1 1.00 SNI 6.50 00000 NA NA NA NA
2 2.00 Sham 6.50 00001 NA NA NA NA
3 3.00 SNI 9.50 00001 NA NA NA NA
4 4.00 Sham 5.50 00000 NA NA NA NA
5 5.00 SNI 8.50 00001 NA NA NA NA
6 6.00 Sham 7.50 00101
I'm looking now to do this more programmatically for as many t-columns as I have.
I think it's easiest to reframe this as joining tables rather than matching. I got a solution with dplyr, purrr, and base::merge() for handling strings. This should scale up for you!
library(dplyr)
library(purrr)
data <- structure(list(Mouse = c(1, 2, 3, 4, 5, 6, 7, 8), Group = c("SNI",
"Sham", "SNI", "Sham", "SNI", "Sham", "SNI", "Sham"), t1 = c("00011",
"00011", "00000", "00110", "00001", "00010", "01001", "00110"
), t2 = c("00000", "00001", "00001", "00000", "00001", "00101",
"00100", "00010")), .Names = c("Mouse", "Group", "t1", "t2"), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
lookup <- structure(list(Response = c("00000", "00001", "00010", "00011",
"00100", "00101", "00110", "00111", "01000", "01001", "01010",
"01011", "01100", "01101", "01110", "01111", "10000", "10001",
"10010", "10011", "10100", "10101", "10110", "10111", "11000",
"11001", "11010", "11011", "11100", "11101", "11110", "1111"),
`Final Fil.` = c(9, 9, 7, 7, 7, 7, 5, 5, 7, 7, 5, 5, 5, 5,
3, 3, 7, 7, 5, 5, 5, 5, 3, 3, 5, 5, 3, 3, 3, 3, 1, 2), Adjustment = c(0.5,
-0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5,
0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5,
-0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5), PWT = c(9.5,
8.5, 7.5, 6.5, 7.5, 6.5, 5.5, 4.5, 7.5, 6.5, 5.5, 4.5, 5.5,
4.5, 3.5, 2.5, 7.5, 6.5, 5.5, 4.5, 5.5, 4.5, 3.5, 2.5, 5.5,
4.5, 3.5, 2.5, 3.5, 2.5, 1.5, 1.5)), .Names = c("Response",
"Final Fil.", "Adjustment", "PWT"), row.names = c(NA, -32L), class = c("tbl_df",
"tbl", "data.frame"))
not_matched <- c("Mouse", "Group")
to_match <- colnames(data)[!colnames(data) %in% not_matched]
to_match
#> [1] "t1" "t2"
lookup_subset <- lookup[c("Response", "PWT")]
lookup_subset
#> # A tibble: 32 x 2
#> Response PWT
#> <chr> <dbl>
#> 1 00000 9.5
#> 2 00001 8.5
#> 3 00010 7.5
#> 4 00011 6.5
#> 5 00100 7.5
#> 6 00101 6.5
#> 7 00110 5.5
#> 8 00111 4.5
#> 9 01000 7.5
#> 10 01001 6.5
#> # ... with 22 more rows
to_match %>%
map_dfc(function(col) {
data[col] %>%
merge(lookup_subset, by.x = col, by.y = "Response", all.x = TRUE) %>%
select(-1) %>%
set_names(col)
}) %>%
bind_cols(data[not_matched], .)
#> # A tibble: 8 x 4
#> Mouse Group t1 t2
#> <dbl> <chr> <dbl> <dbl>
#> 1 1 SNI 9.5 9.5
#> 2 2 Sham 8.5 9.5
#> 3 3 SNI 7.5 8.5
#> 4 4 Sham 6.5 8.5
#> 5 5 SNI 6.5 8.5
#> 6 6 Sham 5.5 7.5
#> 7 7 SNI 5.5 7.5
#> 8 8 Sham 6.5 6.5

how to plot data in time series

I have data that looks like this:
time sucrose fructose glucose galactose molasses water
1 5 0.0 0.00 0.0 0.0 0.3 0
2 10 0.3 0.10 0.1 0.0 1.0 0
3 15 0.8 0.20 0.2 0.2 1.4 0
4 20 1.3 0.35 0.7 0.4 2.5 0
5 25 2.2 0.80 1.6 0.5 3.5 0
6 30 3.1 1.00 2.3 0.6 4.5 0
7 35 3.6 1.60 3.1 0.7 5.7 0
8 40 5.1 2.80 4.3 0.7 6.7 0
How can i make a time series plot that uses the time column? They are all increasing values.
I saw this post multiple-time-series-in-one-plot which uses ts.plot to achieve something similar to what i want to show, which is this:
Input data for the table above:
structure(list(time = c(5, 10, 15, 20, 25, 30, 35, 40), sucrose = c(0,
0.3, 0.8, 1.3, 2.2, 3.1, 3.6, 5.1), fructose = c(0, 0.1, 0.2,
0.35, 0.8, 1, 1.6, 2.8), glucose = c(0, 0.1, 0.2, 0.7, 1.6, 2.3,
3.1, 4.3), galactose = c(0, 0, 0.2, 0.4, 0.5, 0.6, 0.7, 0.7),
molasses = c(0.3, 1, 1.4, 2.5, 3.5, 4.5, 5.7, 6.7), water = c(0,
0, 0, 0, 0, 0, 0, 0)), .Names = c("time", "sucrose", "fructose",
"glucose", "galactose", "molasses", "water"), row.names = c(NA,
-8L), class = "data.frame")
It doesn't seem like a ts plot is necessary. Here's how you could do it in base-R:
with(df, plot(time, sucrose, type="n", ylab="contents"))
var <- names(df)[-1]
for(i in var) lines(df$time, df[,i])
The more elegant solution would however be using the 'dplyrandggplot2` package:
df <- df %>%
gather(content, val, -time)
ggplot(df, aes(time, val, col=content)) + geom_line()

R programming help in editing code

I've asked many questions about this and all the answers were really helpful...but once again my data is weird and I need help...Basically, what I want to do is find the average speed at a certain range of intervals...lets say from 6 s to 40 s my average speed would be 5 m/s...etc etc..
So it was pointed out to me to use this code...
library(IRanges)
idx <- seq(1, ncol(data), by=2)
# idx is now 1, 3, 5. It will be passed one value at a time to `i`.
# that is, `i` will take values 1 first, then 3 and then 5 and each time
# the code within is executed.
o <- lapply(idx, function(i) {
ir1 <- IRanges(start=seq(0, max(data[[i]]), by=401), width=401)
ir2 <- IRanges(start=data[[i]], width=1)
t <- findOverlaps(ir1, ir2)
d <- data.frame(mean=tapply(data[[i+1]], queryHits(t), mean))
cbind(as.data.frame(ir1), d)
})
which gives this output
# > o
# [[1]]
# start end width mean
# 1 0 400 401 1.05
#
# [[2]]
# start end width mean
# 1 0 400 401 1.1
#
# [[3]]
# start end width mean
# 1 0 400 401 1.383333
So if I wanted it to be every 100 s... I'll just change ir1 <- ....., by = 401 to become by=100.
But my data is weird because of a few things
my data doesnt always start with 0 s sometimes it starts at 20 s...depending on the specimen and whether it moves
My data collection does not happen every 1s or 2s or 3s. Hence sometimes I get data 1-20 s but it skips over 20-40 s simply because the specimen does not move.
I think the findOverlaps portion of the code affects my output. How can I get rid of that without disturbing the output?
Here is some data to illustrate my troubles...but all of my real data ends in 2000s
Time Speed Time Speed Time Speed
6.3 1.6 3.1 1.7 0.3 2.4
11.3 1.3 5.1 2.2 1.3 1.3
13.8 1.3 6.3 3.4 3.1 1.5
14.1 1.0 7.0 2.3 4.5 2.7
47.4 2.9 11.3 1.2 5.1 0.5
49.2 0.7 26.5 3.3 5.9 1.7
50.5 0.9 27.3 3.4 9.7 2.4
57.1 1.3 36.6 2.5 11.8 1.3
72.9 2.9 40.3 1.1 13.1 1.0
86.6 2.4 44.3 3.2 13.8 0.6
88.5 3.4 50.9 2.6 14.0 2.4
89.0 3.0 62.6 1.5 14.8 2.2
94.8 2.9 66.8 0.5 15.5 2.6
117.4 0.5 67.3 1.1 16.4 3.2
123.7 3.2 67.7 0.6 26.5 0.9
124.5 1.0 68.2 3.2 44.7 3.0
126.1 2.8 72.1 2.2 45.1 0.8
As you can see from the data, it doesnt necessarily end in 60 s etc sometimes it only ends at 57 etc
EDIT add dput of data
structure(list(Time = c(6.3, 11.3, 13.8, 14.1, 47.4, 49.2, 50.5,
57.1, 72.9, 86.6, 88.5, 89, 94.8, 117.4, 123.7, 124.5, 126.1),
Speed = c(1.6, 1.3, 1.3, 1, 2.9, 0.7, 0.9, 1.3, 2.9, 2.4,
3.4, 3, 2.9, 0.5, 3.2, 1, 2.8), Time.1 = c(3.1, 5.1, 6.3,
7, 11.3, 26.5, 27.3, 36.6, 40.3, 44.3, 50.9, 62.6, 66.8,
67.3, 67.7, 68.2, 72.1), Speed.1 = c(1.7, 2.2, 3.4, 2.3,
1.2, 3.3, 3.4, 2.5, 1.1, 3.2, 2.6, 1.5, 0.5, 1.1, 0.6, 3.2,
2.2), Time.2 = c(0.3, 1.3, 3.1, 4.5, 5.1, 5.9, 9.7, 11.8,
13.1, 13.8, 14, 14.8, 15.5, 16.4, 26.5, 44.7, 45.1), Speed.2 = c(2.4,
1.3, 1.5, 2.7, 0.5, 1.7, 2.4, 1.3, 1, 0.6, 2.4, 2.2, 2.6,
3.2, 0.9, 3, 0.8)), .Names = c("Time", "Speed", "Time.1",
"Speed.1", "Time.2", "Speed.2"), class = "data.frame", row.names = c(NA,
-17L))
sorry if i don't understand your question entirely, could you explain why this example doesn't do what you're trying to do?
# use a pre-loaded data set
mtcars
# choose which variable to cut
var <- 'mpg'
# define groups, whether that be time or something else
# and choose how to cut it.
x <- cut( mtcars[ , var ] , c( -Inf , seq( 15 , 25 , by = 2.5 ) , Inf ) )
# look at your cut points, for every record
x
# you can merge them back on to the mtcars data frame if you like..
mtcars$cutpoints <- x
# ..but that's not necessary
# find the mean within those groups
tapply(
mtcars[ , var ] ,
x ,
mean
)
# find the mean within groups, using a different variable
tapply(
mtcars[ , 'wt' ] ,
x ,
mean
)

Adding a row with Sum and mean of the columns

I'm having a dataframe as like below.
`> am_me
Group.1 Group.2 x.x x.y
2 AM clearterminate 3 21.00000
3 AM display.cryptic 86 30.12791
4 AM price 71 898.00000`
I would like to get result as like below.
`> am_me_t
Group.2 x.x x.y
2 clearterminate 3 21
3 display.cryptic 86 30.1279069767442
4 price 71 898
41 AM 160 316.375968992248`
I have taken out the first column and got the result like below
`> am_res
Group.2 x.x x.y
2 clearterminate 3 21.00000
3 display.cryptic 86 30.12791
4 price 71 898.00000`
When I try rbind to Add "AM" to new row, as like below, I'm getting a warning message and getting NA.
`> am_me_t <- rbind(am_res, c("AM", colSums(am_res[2]), colMeans(am_res[3])))
Warning message:
invalid factor level, NAs generated in: "[<-.factor"(`*tmp*`, ri, value = "AM")
Group.2 x.x x.y
2 clearterminate 3 21
3 display.cryptic 86 30.1279069767442
4 price 71 898
41 <NA> 160 316.375968992248`
For your information, Output of edit(am_me)
`> edit(am_me)
structure(list(Group.1 = structure(as.integer(c(2, 2, 2)), .Label = c("1Y",
"AM", "BE", "CM", "CO", "LX", "SN", "US", "VK", "VS"), class = "factor"),
Group.2 = structure(as.integer(c(2, 5, 9)), .Label = c("bestbuy",
"clearterminate", "currency.display", "display", "display.cryptic",
"fqa", "mileage.display", "ping", "price", "reissue", "reissuedisplay",
"shortaccess.followon"), class = "factor"), x.x = as.integer(c(3,
86, 71)), x.y = c(21, 30.1279069767442, 898)), .Names = c("Group.1",
"Group.2", "x.x", "x.y"), row.names = c("2", "3", "4"), class = "data.frame")`
Also
`> edit(me)
structure(list(Group.1 = structure(as.integer(c(1, 2, 2, 2, 3,
4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8,
8, 8, 9, 9, 10, 10, 10, 10, 10, 10)), .Label = c("1Y", "AM",
"BE", "CM", "CO", "LX", "SN", "US", "VK", "VS"), class = "factor"),
Group.2 = structure(as.integer(c(8, 2, 5, 9, 10, 1, 2, 5,
9, 1, 2, 5, 9, 1, 2, 3, 4, 7, 9, 11, 12, 2, 4, 6, 1, 2, 5,
9, 2, 5, 1, 2, 3, 5, 9, 10)), .Label = c("bestbuy", "clearterminate",
"currency.display", "display", "display.cryptic", "fqa",
"mileage.display", "ping", "price", "reissue", "reissuedisplay",
"shortaccess.followon"), class = "factor"), x.x = as.integer(c(1,
3, 86, 71, 1, 2, 5, 1, 52, 10, 7, 27, 15, 5, 267, 14, 4,
1, 256, 1, 1, 80, 1, 78, 2, 10, 23, 6, 1, 2, 4, 3, 3, 11,
1, 1)), x.y = c(5, 21, 30.1279069767442, 898, 12280, 800,
56.4, 104, 490.442307692308, 1759.1, 18.1428571428571, 1244.81481481481,
518.533333333333, 3033.2, 18.5468164794007, 20, 3788.5, 23,
2053.49609375, 3863, 6376, 17.825, 240, 1752.21794871795,
1114.5, 34, 1369.60869565217, 1062.16666666667, 23, 245,
5681.5, 11.3333333333333, 13.3333333333333, 1273.81818181818,
2076, 5724)), .Names = c("Group.1", "Group.2", "x.x", "x.y"
), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
"32", "33", "34", "35", "36"), class = "data.frame")
Group.1 Group.2 x.x x.y
1 1Y ping 1 5.00000
2 AM clearterminate 3 21.00000
3 AM display.cryptic 86 30.12791
4 AM price 71 898.00000
5 BE reissue 1 12280.00000
6 CM bestbuy 2 800.00000
7 CM clearterminate 5 56.40000
8 CM display.cryptic 1 104.00000
9 CM price 52 490.44231
10 CO bestbuy 10 1759.10000
11 CO clearterminate 7 18.14286
12 CO display.cryptic 27 1244.81481
13 CO price 15 518.53333
14 LX bestbuy 5 3033.20000
15 LX clearterminate 267 18.54682
16 LX currency.display 14 20.00000
17 LX display 4 3788.50000
18 LX mileage.display 1 23.00000
19 LX price 256 2053.49609
20 LX reissuedisplay 1 3863.00000
21 LX shortaccess.followon 1 6376.00000
22 SN clearterminate 80 17.82500
23 SN display 1 240.00000
24 SN fqa 78 1752.21795
25 US bestbuy 2 1114.50000
26 US clearterminate 10 34.00000
27 US display.cryptic 23 1369.60870
28 US price 6 1062.16667
29 VK clearterminate 1 23.00000
30 VK display.cryptic 2 245.00000
31 VS bestbuy 4 5681.50000
32 VS clearterminate 3 11.33333
33 VS currency.display 3 13.33333
34 VS display.cryptic 11 1273.81818
35 VS price 1 2076.00000
36 VS reissue 1 5724.00000`
The type of the Group.2 column is factor, and that limits the possible values. You can transform it to character with am_me$Group.2 <- as.character(am_me$Group.2), after that the AM value will be added without errors.
Note that you can also use sum() and mean() for single column operations.

Resources