Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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_Dataframe_Data.table_Dplyr - Fatal编程技术网

R 创建使用下划线连接的测量值组合

R 创建使用下划线连接的测量值组合,r,dataframe,data.table,dplyr,R,Dataframe,Data.table,Dplyr,我有一个数据帧df1 ID <- c("A","B","C") Measurement <- c("Length","Height","Breadth") df1 <- data.frame(ID,Measurement) 另外,当“度量”列中有类似的度量时,我希望消除下划线 例如: ID <- c("A","B") Measurement <- c("Length","Length") df2 <- data.frame(ID,Measurement)

我有一个数据帧df1

ID <- c("A","B","C")
Measurement <- c("Length","Height","Breadth")
df1 <- data.frame(ID,Measurement)
另外,当“度量”列中有类似的度量时,我希望消除下划线

例如:

ID <- c("A","B")
Measurement <- c("Length","Length")
df2 <- data.frame(ID,Measurement)
我试图做这样的事情,这是完全错误的

df1$ID <- paste(df1$Measurement, df1$Measurement, sep="_")

df1$ID我们可以从
combinat
软件包中使用
permn
功能:

library(combinat)
sol_1 <- sapply(permn(unique(df1$Measurement)), 
                FUN = function(x) paste(x, collapse = '_'))
rbind.data.frame(df1, data.frame('ID' = 'All', 'Measurement' = sol_1))

#    ID           Measurement
# 1   A                Length
# 2   B                Height
# 3   C               Breadth
# 4 All Length_Height_Breadth
# 5 All Length_Breadth_Height
# 6 All Breadth_Length_Height
# 7 All Breadth_Height_Length
# 8 All Height_Breadth_Length
# 9 All Height_Length_Breadth

sol_2 <- sapply(permn(unique(df2$Measurement)), 
                FUN = function(x) paste(x, collapse = '_'))
rbind.data.frame(df2, data.frame('ID' = 'All', 'Measurement' = sol_2))

#    ID Measurement
# 1   A      Length
# 2   B      Length
# 3 All      Length

gtools
df1$ID <- paste(df1$Measurement, df1$Measurement, sep="_")
library(combinat)
sol_1 <- sapply(permn(unique(df1$Measurement)), 
                FUN = function(x) paste(x, collapse = '_'))
rbind.data.frame(df1, data.frame('ID' = 'All', 'Measurement' = sol_1))

#    ID           Measurement
# 1   A                Length
# 2   B                Height
# 3   C               Breadth
# 4 All Length_Height_Breadth
# 5 All Length_Breadth_Height
# 6 All Breadth_Length_Height
# 7 All Breadth_Height_Length
# 8 All Height_Breadth_Length
# 9 All Height_Length_Breadth

sol_2 <- sapply(permn(unique(df2$Measurement)), 
                FUN = function(x) paste(x, collapse = '_'))
rbind.data.frame(df2, data.frame('ID' = 'All', 'Measurement' = sol_2))

#    ID Measurement
# 1   A      Length
# 2   B      Length
# 3 All      Length
library(gtools)
unique_meas <- as.character(unique(df1$Measurement))
apply(permutations(length(unique_meas), length(unique_meas), unique_meas),
      1, FUN = function(x) paste(x, collapse = '_'))

# "Breadth_Height_Length" "Breadth_Length_Height" 
# "Height_Breadth_Length" "Height_Length_Breadth"
# "Length_Breadth_Height" "Length_Height_Breadth"