Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中轻松安全地重塑数据?_R_Reshape - Fatal编程技术网

如何在R中轻松安全地重塑数据?

如何在R中轻松安全地重塑数据?,r,reshape,R,Reshape,通常我的数据是“宽格式”的,同一个变量有几列(请参阅),我希望数据是长格式的(请参阅)。你有什么简单安全的解决方案吗 由于您的两个示例没有相同的数字,不知道这是否是您想要的: data <- read.table(text='"id","name1","name2","x1","x2" 1,"Mr a","Mr c","0.575710014459387","-0.991747242336918" 2,"Mr b","Mr d","-0.126033858727122","0.854792

通常我的数据是“宽格式”的,同一个变量有几列(请参阅),我希望数据是长格式的(请参阅)。你有什么简单安全的解决方案吗

由于您的两个示例没有相同的数字,不知道这是否是您想要的:

data <- read.table(text='"id","name1","name2","x1","x2"
1,"Mr a","Mr c","0.575710014459387","-0.991747242336918"
2,"Mr b","Mr d","-0.126033858727122","0.854792650572792"', sep=',', header=TRUE)

reshape(data, direction = 'long', idvar = 'id', varying = list(c('x1', 'x2'), c('name1', 'name2')), times=c('a', 'b'))

数据或使用melt功能取消PIVOT:

library(reshape2)
data <- read.table(text='"id","name1","name2","x1","x2","Mr a","Mr c","0.575710014459387","-0.991747242336918","Mr b","Mr d","-0.126033858727122","0.854792650572792"', sep=',', header=TRUE)
reshape(data, direction = 'long', idvar = 'id', varying = list(c('x1', 'x2'), c('name1', 'name2')), times=c('a', 'b'))
melt(data,c("id","name1","name2"),variable.name="x",value.name="value")
library(重塑2)
数据“安全”是什么意思?