我有很多列的数据,我想将其转换为igraph对象。一些值存在于几列中,因此我想添加列名以标识那些值。下面是一个可重现的示例。
# sample of original data
head(as.data.frame(Titanic))
# sample of the data after the col name has been appended to the value of the column
data.frame(Class = c("Class_1st","Class_2nd","Class_3rd","Class_Crew","Class_1st","Class_2nd"),
Sex = c("Sex_Male","Sex_Male","Sex_Male","Sex_Male","Sex_Female","Sex_Female"),
Age = c("Age_Child","Age_Child","Age_Child","Age_Child","Age_Child","Age_Child"),
Survived = c("Survived_No","Survived_No","Survived_No","Survived_No","Survived_No","Survived_No"),
Freq = c(0,0,35,0,0,0), stringsAsFactors = FALSE)
答案 0 :(得分:1)
不确定这是否是您想要的,但是否与示例输出匹配
df <- head(as.data.frame(Titanic))
df[,1:4] <- paste(names(df[,1:4])[col(df[,1:4])], unlist(df[,1:4]), sep="_")