0

ベクトルに結合したい列のリストがあります。列要素は、名前または文字列「0」です。と呼ばれる文字ベクトルに名前を持つ列の要素のリストを取得したいと思いますdf$keywords。以下にデータフレームの例を貼り付けました。になってほしい

df$keywords[1,]空のベクトルになります

df$keywords[2,]( ACT Science, study skills, MCAT)

どんな助けでもいただければ幸いです

    structure(list(V31 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L), .Label = "0", class = "factor"), V32 = structure(c(1L, 
    2L, 4L, 5L, 7L, 8L, 6L, 5L, 3L, 3L), .Label = c("0", "ACT Science", 
    "English", "Microsoft PowerPoint", "physics", "proofreading", 
    "reading", "writing"), class = "factor"), V33 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "0", class = "factor"), 
    V34 = structure(c(1L, 7L, 5L, 5L, 8L, 2L, 6L, 5L, 3L, 4L), .Label = c("0", 
    "geography", "Italian", "literature", "prealgebra", "SAT reading", 
    "study skills", "trigonometry"), class = "factor"), V35 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "0", class = "factor"), 
    V36 = structure(c(1L, 3L, 4L, 4L, 7L, 2L, 6L, 4L, 5L, 5L), .Label = c("0", 
    "English", "MCAT", "precalculus", "proofreading", "SAT writing", 
    "writing"), class = "factor"), V37 = structure(c(1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "0", class = "factor"), 
    V38 = structure(c(1L, 1L, 5L, 5L, 2L, 1L, 4L, 5L, 3L, 6L), .Label = c("0", 
    "English", "GED", "physical science", "reading", "spelling"
    ), class = "factor")), .Names = c("V31", "V32", "V33", "V34", 
    "V35", "V36", "V37", "V38"), class = "data.frame", row.names = c(NA, 
    -10L))
4

2 に答える 2

1

最初の投稿では、必要な出力を正しく理解していませんでした。少し異なるアプローチは、次の%in%ように行全体で演算子を使用することです。

df$keywords <- apply(df,1, function(x) c( x[! x %in% "0"]))
df$keywords
#                                                                                                            keywords
#1                                                                                                                    
#2                                                    ACT Science, study skills, MCAT, ACT Science, study skills, MCAT
#3      Microsoft PowerPoint, prealgebra, precalculus, reading, Microsoft PowerPoint, prealgebra, precalculus, reading
#4                                physics, prealgebra, precalculus, reading, physics, prealgebra, precalculus, reading
#5                                    reading, trigonometry, writing, English, reading, trigonometry, writing, English
#6                                                            writing, geography, English, writing, geography, English
#7  proofreading, SAT reading, SAT writing, physical science, proofreading, SAT reading, SAT writing, physical science
#8                                physics, prealgebra, precalculus, reading, physics, prealgebra, precalculus, reading
#9                                            English, Italian, proofreading, GED, English, Italian, proofreading, GED
#10                           English, literature, proofreading, spelling, English, literature, proofreading, spelling

unique行ごとに一連のスキルが必要な場合は、次uniqueのようにコマンドを追加します。

df$keywords <- apply(df,1, function(x) c( unique(x[ ! x %in% "0" ] ) ) )
df["keywords"]
#                                                  keywords
#1                                                          
#2                           ACT Science, study skills, MCAT
#3    Microsoft PowerPoint, prealgebra, precalculus, reading
#4                 physics, prealgebra, precalculus, reading
#5                   reading, trigonometry, writing, English
#6                               writing, geography, English
#7  proofreading, SAT reading, SAT writing, physical science
#8                 physics, prealgebra, precalculus, reading
#9                       English, Italian, proofreading, GED
#10              English, literature, proofreading, spelling
于 2013-04-28T06:15:38.473 に答える