Chris95
Chris95

Reputation: 75

Converting data types in R

I want to understand how to transform the first data type into a data type I can work with. Here is the code I am using, basically I'm just trying to do OLS regression on the spam data set and then compute the std error and test error. However I am having trouble converting the data types below. Here is the code so you can reproduce the example. The spam and test data can be found at this link: https://statweb.stanford.edu/~tibs/ElemStatLearn/ (go to data tab on the left and then scroll down to spam data)

# reading in the data
spam <- read.table("spam.data.txt")     # X
test.set   <- read.table("test.set.txt")  # tt

# train and test set
x.train = subset(spam, test.set==0) # assigning the train to 0
p = dim(x.train)[2]-1               # dimension 
x.test = subset(spam, test.set==1)  # assigning the test to 1

# standardization of predictors
trainst <- x.train
for(i in 1:58) {
  trainst[,i] <- trainst[,i] - mean(spam[,i]);
  trainst[,i] <- trainst[,i]/sd(spam[,i]);
}
testst <- x.test
for(i in 1:58) {
  testst[,i] <- testst[,i] - mean(spam[,i]);
  testst[,i] <- testst[,i]/sd(spam[,i]);
}

# permuting data frames
train = trainst[sample(nrow(trainst)),]
test = testst[sample(nrow(testst)),]
library("MASS")
fit.ls <- lm(train$V58 ~ . - 1, train)
ls <- fit.ls$coef

This bottom line fit.ls$coef is what produces the list of numbers below. I basically want to convert that data type so I can use it to compute the std error and test error.

enter image description here

Upvotes: 0

Views: 196

Answers (2)

shiny
shiny

Reputation: 3502

Try this

df <- do.call(rbind, lapply(ls, data.frame, stringsAsFactors=F))


head(df)
#        X..i..
#V1 -0.02287274
#V2 -0.03491058
#V3  0.05197059
#V4  0.03802391
#V5  0.10026819
#V6  0.07542830

You can easily now calculate sd

df %>% dplyr::rename(value = X..i..) %>% mutate(sd = sd(value))

Upvotes: 2

Jeremy Farrell
Jeremy Farrell

Reputation: 1491

You can acheive this with reshape2.

library(reshape2)
data = c(v1 = 5, v2= 3, v3= 7)
melted = melt(data)

Upvotes: 3

Related Questions