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