在R中转置data.frame并将其中一列设置为新转置表的标头的最佳方法是什么?我编写了一种方法来执行此操作.因为我还是R的新手.我想建议改进我的代码以及更像R的替代品.不幸的是,我的解决方案也有点硬编码(即新的列标题位于某个位置).
# Assume a data.frame called fooData
# Assume the column is the first column before transposing
# Transpose table
fooData.T <- t(fooData)
# Set the column headings
colnames(fooData.T) <- test[1,]
# Get rid of the column heading row
fooData.T <- fooData.T[2:nrow(fooData.T), ]
#fooData.T now contains a transposed table with the first column as headings
Run Code Online (Sandbox Code Playgroud) 我想在变量(列)之间生成图形,这些图形具有高于和低于某个点的相关性以及p值<0.01.图表将是绘制相关的两列(变量)的ggplot2(线或条)图.
到目前为止,这是我的方法的要点,有一些虚拟数据,我会喜欢指向下一步的指针.
# Create some dummy data
df <- data.frame(sample(1:50), sample(1:50), sample(1:50), sample(1:50))
colnames(df) <- c("var1", "var2", "var3", "var4")
# Find correlations in the dummy data
df.cor <- cor(df)
# Make up some random pvalues for this example
x <- 0:1000
df.cor.pvals <- data.frame(sample(x/1000, 4), sample(x/1000, 4), sample(x/1000, 4), sample(x/1000,4))
colnames(df.cor.pvals) <- c("var1", "var2", "var3", "var4")
# Find the significant correlations
df.cor.extreme <- ((df.cor < -0.01 | df.cor > 0.01) & df.cor.pvals < 0.5)
# Ready data to for plotting …Run Code Online (Sandbox Code Playgroud)