2

I would like to turn a print function output into a dataframe object.I used this code but I get only the last line as a dataframe row. Here is a toy dataframe:

df <- read.table(text = " target birds    wolfs     
                 32         9         7 
                 56         8         4 
                 11         2         8 
                 22         2         3 
                 33         8         3 
                 54         1         2 
                 34         7         16 
                 66         1         5 
                 74         17        7 
                 52         8         7 
                 45         2         7 
                 65         20        3 
                 99         6         3 
                 88         1         1 
                 77         3         11 
                 55         30         1  ",header = TRUE)

 for(i in names(df))

     { 
        fit <- lm(df[,i] ~ target, data=df) #does a regression per column 
         res<- summary(fit)$r.squared
         b<-print(paste(res,i))            
     }
# I got this output from the print function :
[1] "1 target"
[1] "0.0110699859137896 birds"
[1] "0.07231285430469 wolfs"

How can I turn this output to a data frame that contains two columns: the first one is for the value (1,0.11,0.07) and the second one is for the names (target,birds,wolfs) I tried to use this command but I got only the las line of the print output:

b1<-as.data.frame(b)
b1
                       b
1 0.07231285430469 wolfs

3 Answers 3

3

I would just run a simple sapply

data.frame(Col = names(df), 
           Rsqrd = unname(sapply(df, function(x) summary(lm(x ~ df$target))$r.squared)))
#      Col      Rsqrd
# 1 target 1.00000000
# 2  birds 0.01106999
# 3  wolfs 0.07231285
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @David Arenburg , one row code that works great.
1

I would save everything into a list and then flatten it into a data.frame.

Something along the lines of

N <- 10

my.list <- vector("list", N)

for (i in 1:N) {
  my.list[[i]] <- c(element1 = runif(1), element2 = runif(1))

do.call("rbind", my.list)
        element1   element2
 [1,] 0.63435664 0.57285398
 [2,] 0.04031899 0.06093284
 [3,] 0.63446004 0.03669334
 [4,] 0.27712280 0.80234739
 [5,] 0.08285555 0.92606686
 [6,] 0.17928155 0.93633017
 [7,] 0.04661537 0.08380414
 [8,] 0.41108456 0.18566136
 [9,] 0.27616150 0.38485649
[10,] 0.21108018 0.87233677

Comments

1

You could change a bit your function :

b<-as.data.frame(matrix(NA,nrow=length(names(df)),ncol=2))
# or b<-data.frame(Rsq=numeric(),Name=character(),stringsAsFactors=FALSE) 

for (i in seq_along(names(df))){
    j<-names(df)[i]
    fit <- lm(df[,j] ~ target, data=df) 
    rsq <- summary(fit)$r.squared
    b[i,]<-cbind(rsq,j)        
}

b
                  V1     V2
1                  1 target
2 0.0110699859137896  birds
3   0.07231285430469  wolfs

What didn't work in your function was the fact that b changed at each iteration and the previous values were removed. Here the values are put in the dataframe and you can use it later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.