我有以下示例数据:
"","Class","Sex","Age","Survived","Freq"
"1","1st","Male","Child","No",0
"2","2nd","Male","Child","No",0
"3","3rd","Male","Child","No",2
"4","Crew","Male","Child","No",0
我使用以下内容将其存储在R中的列表中:
dat = read.csv("File.csv", header = TRUE)
现在我想将此列表复制到另一个没有" Freq"列,但根据" Freq"的值有更多行。 (请参阅下面的reqd。数据)(Freq = 0无效):
"","Class","Sex","Age","Survived"
"1","1st","Male","Child","No"
"2","2nd","Male","Child","No"
"3","3rd","Male","Child","No"
"3","3rd","Male","Child","No"
"4","Crew","Male","Child","No"
由于其Freq = 2,原始数据中的第3行在新数据中加倍。但是,Freq = 0的行在输出数据中仍然有1行。任何帮助将不胜感激。
答案 0 :(得分:2)
我们可以使用rep
使用'Freq'列复制数据集的行序列。由于'Freq'中有0个值,我们需要replace
1
并使用该向量作为rep
中的参数,根据输出扩展'dat'行rep
以及使用setdiff
选择不属于“频率”的列。
dat[rep(1:nrow(dat), replace(dat$Freq, dat$Freq==0, 1)), setdiff(names(dat), "Freq")]
# Class Sex Age Survived
#1 1st Male Child No
#2 2nd Male Child No
#3 3rd Male Child No
#3.1 3rd Male Child No
#4 Crew Male Child No
dat <- structure(list(Class = c("1st", "2nd", "3rd", "Crew"), Sex = c("Male",
"Male", "Male", "Male"), Age = c("Child", "Child", "Child", "Child"
), Survived = c("No", "No", "No", "No"), Freq = c(0L, 0L, 2L,
0L)), .Names = c("Class", "Sex", "Age", "Survived", "Freq"), class =
"data.frame", row.names = c(NA, -4L))