Rokib
Rokib

Reputation: 127

Adding significant stars' row with existing output in R

I got column means and range(min, max) from my data.

df=matrix(c(3, 5, 2, 3, 6, 3,4, 4, 4, 5, 4, 3,5, 5, 5),ncol=3,byrow=TRUE)
colnames(df)<-paste0("ch", 1:ncol(df))
rownames(df)<-paste0("G", 1:nrow(df))
mean<- colMeans(df, na.rm = FALSE, dims = 1)
range<-apply(df, 2, range)
rownames(range) <- c("min","max")
res<-rbind(mean,range)

I have a standard mean value(4). Now I want to add additional row showing significant marks(**) with the existing output. Mean values less than 4 were considered significant. Somehow I got significant marks but I failed to add this with the existing result.

 f<-res[1,] <4
test <- factor(f, labels=c("Ns", "**"))
result<-rbind(mean,range,test)
result
     ch1 ch2 ch3
mean   4 4.8 3.4
min    3 4.0 2.0
max    5 6.0 5.0
test   1 1.0 2.0

I wanted this like following one

     ch1 ch2 ch3
mean   4 4.8 3.4
min    3 4.0 2.0
max    5 6.0 5.0
test   Ns Ns **

I need your help to solve this.

Upvotes: 1

Views: 282

Answers (2)

deepseefan
deepseefan

Reputation: 3791

rbind.data.frame(mean = mean, range, test = as.character(test))

#       ch1 ch2 ch3
# mean   4 4.8 3.4
# min    3   4   2
# max    5   6   5
# test  Ns  Ns  **

See ?rbind.data.frame for a detail.

Upvotes: 2

Hailong Ji
Hailong Ji

Reputation: 53

I think, Matrix can only store the data that have same type. Here, the first three rows are numeric. However, the test is factor, and it's coerced to numeric, that Ns and ** mapping to 1 and 2. I suggest you should use data.frame to do it.

res<-rbind(mean,range)
res<-data.frame(t(res))
f<-res[1,] <4
test <- factor(f, labels=c("Ns", "**"))
res<-cbind(res,test)

I hope this ansewer can help you!

Upvotes: 1

Related Questions