R-如何创建具有xy坐标的向量数据帧
假设我有以下两个数据帧R-如何创建具有xy坐标的向量数据帧,r,dataframe,coordinates,R,Dataframe,Coordinates,假设我有以下两个数据帧 dfx <- data.frame(z = rpois(10,2), q = rpois(10,2), p = rpois(10,2), k = rpois(10,2), t = rpois(10,2)) 非常感谢您的帮助。我们可以粘贴 out <- data.frame(Map(paste, dfx, dfy, MoreArgs = list(sep=","))) names(out) <- paste0("column", seq_along(ou
dfx <- data.frame(z = rpois(10,2), q = rpois(10,2), p = rpois(10,2), k = rpois(10,2), t = rpois(10,2))
非常感谢您的帮助。我们可以粘贴
out <- data.frame(Map(paste, dfx, dfy, MoreArgs = list(sep=",")))
names(out) <- paste0("column", seq_along(out))
out
# column1 column2 column3 column4 column5
#1 2,2 1,6 1,0 1,0 1,5
#2 3,2 3,3 1,2 2,2 0,1
#3 3,3 1,0 2,2 1,3 1,1
#4 2,2 5,3 2,4 4,1 1,0
#5 3,1 4,1 1,3 1,1 0,1
#6 4,1 1,2 3,2 2,1 1,5
#7 5,2 5,0 0,2 4,3 2,1
#8 3,0 2,5 4,2 5,4 4,2
#9 3,3 3,2 0,3 5,1 0,3
#10 4,2 2,2 0,1 4,3 5,1
或者,如果将其转换为矩阵
,则可以直接应用粘贴
array(paste(as.matrix(dfx), as.matrix(dfy), sep=","), dim = dim(dfx),
dimnames = list(NULL, paste0("column", seq_along(dfx))))
使用tidyverse
,我们可以使用map2
library(purrr)
library(stringr)
map2_df(dfx, dfy, str_c, sep=",") %>%
rename_all(~ str_c('column', seq_along(.)))
一个base R
选项可以是:
df <- data.frame(matrix(paste(unlist(dfx), unlist(dfy), sep = ","), dim(dfx)))
names(df) <- paste0("column", 1:length(df))
column1 column2 column3 column4 column5
1 1,1 2,2 3,2 2,0 0,3
2 1,3 1,4 3,1 2,1 1,4
3 2,0 0,4 4,1 2,1 2,4
4 3,3 3,3 2,2 1,3 1,2
5 2,2 2,3 0,1 0,0 0,3
6 1,0 0,1 2,4 5,3 4,3
7 0,1 1,3 2,3 0,2 4,3
8 0,1 0,2 1,4 2,3 1,1
9 1,6 2,3 1,0 5,3 0,2
10 3,1 1,2 0,3 3,2 2,1
df
array(paste(as.matrix(dfx), as.matrix(dfy), sep=","), dim = dim(dfx),
dimnames = list(NULL, paste0("column", seq_along(dfx))))
library(purrr)
library(stringr)
map2_df(dfx, dfy, str_c, sep=",") %>%
rename_all(~ str_c('column', seq_along(.)))
df <- data.frame(matrix(paste(unlist(dfx), unlist(dfy), sep = ","), dim(dfx)))
names(df) <- paste0("column", 1:length(df))
column1 column2 column3 column4 column5
1 1,1 2,2 3,2 2,0 0,3
2 1,3 1,4 3,1 2,1 1,4
3 2,0 0,4 4,1 2,1 2,4
4 3,3 3,3 2,2 1,3 1,2
5 2,2 2,3 0,1 0,0 0,3
6 1,0 0,1 2,4 5,3 4,3
7 0,1 1,3 2,3 0,2 4,3
8 0,1 0,2 1,4 2,3 1,1
9 1,6 2,3 1,0 5,3 0,2
10 3,1 1,2 0,3 3,2 2,1