我在数据框df1中有列(契约),其中每个字段都有垂直连接的堆叠名称,如下所示:
Insurance
Environmental
Use of Loans
Chg Fiscal Yr
ERISA
Cons,Merger,Acq
Inventory
Debt
Affiliate Trans
Liens,Encum,Mtg
Collat,Ast Sale
Debt-EBITDA
FixChg Cov
Capital Expend
Capital Expend
Debt Services
Int Cov
Int Cov
Capial Expend
Int Cov
我想计算垂直堆叠名称的数量。例如,关于堆叠字段有20个名称堆叠。因此,它应该返回20.据我所知,splitstackshape
包用于分割堆叠数据,但想知道如何计算堆叠名称,如上所述。
非常感谢您的帮助。
答案 0 :(得分:1)
如果我理解您的数据格式正确,这应该有效,并提供与x大小相同的数据框,每个单元格中的项目数为值。
newdf <- data.frame(
# iterate over columns
lapply(df1, function(y){
# iterate over rows
unname(sapply(y, function(x){
# split at linebreak and get vector length
length(strsplit(x, "\n")[[1]])
}))
})
)
答案 1 :(得分:1)
拆分新线然后得到长度:
# dummy data: with 2 rows for x, 1st has 5 names, 2nd has 8 names.
df1 <- data.frame(covenant = c("Insurance
Environmental
Use of Loans
Chg Fiscal Yr
ERISA",
"Insurance
Environmental
Use of Loans
Chg Fiscal Yr
ERISA
Cons,Merger,Acq
Inventory
Debt"), stringsAsFactors = FALSE)
# split on new line then get the lengths
lengths(lapply(df1$covenant, function(i) unlist(strsplit(i, split = "\n"))))
# [1] 5 8