逐个单元地连接两个矩阵或数据帧(concatenate two matrices or dataframes cell

编程入门 行业动态 更新时间:2024-10-26 10:28:47
逐个单元地连接两个矩阵或数据帧(concatenate two matrices or dataframes cell-by-cell)

我想连接df和p ,如我的MRE的最后一步所示。 有任何想法吗?

# functions ---- asterisk <- function(y) symnum(y, c(0, .001, .01, .05, .1, 1), c("***", "**", "*", ".", " ")) rd2 <- function(y) sprintf("%.2f", round(y, 2)) # packages ---- require(foreign) require(nnet) # example data from http://www.ats.ucla.edu/stat/r/dae/mlogit.htm ml <- read.dta("http://www.ats.ucla.edu/stat/data/hsbdemo.dta") ml$prog2 <- relevel(ml$prog, ref = "academic") test <- multinom(prog2 ~ ses + write, data = ml) # get coefficients and round df <- data.frame(coefficients(test)) df <- as.data.frame(lapply(df, rd2)) row.names(df) <- row.names(coefficients(test)) df # X.Intercept. sesmiddle seshigh write # general 2.85 -0.53 -1.16 -0.06 # vocation 5.22 0.29 -0.98 -0.11 # get p-values z <- summary(test)$coefficients/summary(test)$standard.errors p <- matrix((1 - pnorm(abs(z), 0, 1)) * 2, nrow=nrow(z)) p <- as.data.frame(apply(p, c(1,2), asterisk)) # V1 V2 V3 V4 # 1 * * ** # 2 *** . *** # concatenate df and p # X.Intercept. sesmiddle seshigh write # general 2.85* -0.53 -1.16* -0.06** # vocation 5.22*** 0.29 -0.98. -0.11***

I want to concatenate df and p as shown in the last step of my MRE. Any ideas?

# functions ---- asterisk <- function(y) symnum(y, c(0, .001, .01, .05, .1, 1), c("***", "**", "*", ".", " ")) rd2 <- function(y) sprintf("%.2f", round(y, 2)) # packages ---- require(foreign) require(nnet) # example data from http://www.ats.ucla.edu/stat/r/dae/mlogit.htm ml <- read.dta("http://www.ats.ucla.edu/stat/data/hsbdemo.dta") ml$prog2 <- relevel(ml$prog, ref = "academic") test <- multinom(prog2 ~ ses + write, data = ml) # get coefficients and round df <- data.frame(coefficients(test)) df <- as.data.frame(lapply(df, rd2)) row.names(df) <- row.names(coefficients(test)) df # X.Intercept. sesmiddle seshigh write # general 2.85 -0.53 -1.16 -0.06 # vocation 5.22 0.29 -0.98 -0.11 # get p-values z <- summary(test)$coefficients/summary(test)$standard.errors p <- matrix((1 - pnorm(abs(z), 0, 1)) * 2, nrow=nrow(z)) p <- as.data.frame(apply(p, c(1,2), asterisk)) # V1 V2 V3 V4 # 1 * * ** # 2 *** . *** # concatenate df and p # X.Intercept. sesmiddle seshigh write # general 2.85* -0.53 -1.16* -0.06** # vocation 5.22*** 0.29 -0.98. -0.11***

最满意答案

如果您不介意用连接的数据覆盖现有的df :

df[] <- lapply(1:ncol(df), function(col) paste0(df[,col],p[,col]) );

如果没有,你可以从df分配一个新的变量(比如dfp )并赋值。

使用原始df作为此任务的起点非常有用,因为它可以避免担心设置/修复维名称。

If you don't mind overwriting the existing df with the concatenated data:

df[] <- lapply(1:ncol(df), function(col) paste0(df[,col],p[,col]) );

If not, you can assign a new variable from df (say dfp) and assign that.

Using the original df as a starting point for this task is useful because it prevents having to worry about setting/fixing dimension names.

更多推荐

本文发布于:2023-08-06 23:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   单元   两个   数据   dataframes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!