R在数据帧中的每个单元格上应用函数

时间:2016-02-14 12:11:01

标签: r dataframe apply

我有一个看起来像这样的数据框

> dput(tes)
structure(list(path = structure(1:6, .Label = c("1893-chicago-fair", 
"1960s-afghanistan", "1970s-iran", "1980s-new-york", "20-bizarre-vintage-ads", 
"20-bizarre-vintage-ads?utm_campaign=6678&utm_medium=rpages&utm_source=Facebook&utm_term=1e8e704f7b587515c72e6cf7895d55fd110b652c480d98c1440f0a7acba5fb0e", 
"20-photos-segregation-america-show-far-weve-come-much-farther-go", 
"7-bizarre-cultural-practices", "7-creepy-abandoned-cities?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=4015a7368b588ff09694c96ba720c58f4e7f41a05b4181908b582bae682bef5e", 
"a-brief-history-of-hippies", "abandoned-photographs", "albert-kahn", 
"amazing-facts", "american-bison-extinction-1800s", "american-english-vs-british-english", 
"andre-the-giant-photos", "andre-the-giant-photos??utm_source=facebook&sr_source=lift_facebook&utm_campaign=simplereach_andre-the-giant-photos&utm_medium=social", 
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e", 
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e&utm_campaign=gravityus2_142deb68f67fb1a99e7b80250fecc932&utm_medium=referral&utm_source=gravity", 
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e&utm_campaign=gravityus2_16d63cf07ecf656f602b2d6b209344f7&utm_medium=referral&utm_source=gravity", 
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e&utm_campaign=gravityus2_713050ecffc51540af02b2246ddf57dd&utm_medium=referral&utm_source=gravity", 
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e&utm_campaign=gravityus2_c5bb3bc5e9408e0ad52ec9e787bd8654&utm_medium=referral&utm_source=gravity", 
"andre-the-giant-photos?sr_source=lift_facebook&utm_campaign=simplereach_andre-the-giant-photos&utm_medium=social&utm_source=facebook", 
"astounding-aerial-photography", "astounding-aerial-photography?utm_campaign=7002&utm_medium=rpages&utm_source=Facebook&utm_term=38e9e903d9ba59106d8b4d19be593f3de7ff8b91b12eafa03f2e382228f7b0d1", 
"august-landmesser", "ben-franklin", "best-all-that-is-interesting-articles", 
"bigfoot-facts", "celebrity-school-photos?grvVariant=82c0ce57a33dfd0209bdefc878665de0&utm_campaign=gravityus2_bc8646aefd6d0a16af03d7caf248f226&utm_medium=referral&utm_source=gravity", 
"coolest-mushrooms?utm_campaign=taboolaINTL&utm_medium=referral&utm_source=taboola", 
"craziest-ways-drugs-smuggled", "creepy-halloween-costumes", 
"danakil-depression", "dark-john-lennon-quotes", "david-bowie-quotes", 
"days-in-groundhog-day", "death-photos", "death-photos?utm_campaign=taboolaINTL&utm_medium=referral&utm_source=taboola", 
"dr-seuss-quotes", "dream-chaser-spacecraft", "dust-bowl", "earth-two-planets", 
"eixample-barcelona", "email-to-space", "evil-science-experiments", 
"famous-incest", "famous-spies", "fun-facts-trivia", "golden-age-air-travel?utm_campaign=taboolaINTL&utm_medium=referral&utm_source=taboola", 
"gross-foods", "gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=106965c54919c24bf37356500ec50f0709b1de621d6950bb4c5d48759ea3677e", 
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=184e0ee39e66af82f9b124b904f6e07964b211e902cb0dc00c28771ff46163a2", 
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=1a0ddea7bed770d5473c45e9f8d81dfd0c4fdd232f207c6b88b53c41ff220c59", 
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=538659f1fc53f28d2c87b93ac73973681c1a46a04954964ab6c52ed1ab09b33a", 
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=87caf0acb91ae2b202f1b00ad9eaad3fef20bbfb23405b9047fb2b5a5462ab9c", 
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=91eae42c8fc9568103d46e0b6b6ec08fc34fd68b2e1918ffe2333ec73035c95a", 
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=a72946874b2003a8e40635c6cf10c851d4e1c0ed45e645d69663214239550602", 
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=ab594f0a1be002c8c3db297e8d33b04678af40e6a6469ac815884ae0a014b3a3", 
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=fb1e333dd58cb7bb9251ec52290aae21771149f73e083440047068a69aaeae09", 
"hilarious-insults", "hippie-communes", "hippie-communes?grvVariant=fda07538efb1c25617f7cc3d09c37c79", 
"hippie-communes?grvVariant=fda07538efb1c25617f7cc3d09c37c79&utm_campaign=gravityus2_e3cd42d4745768460dab4694a972fd82&utm_medium=referral&utm_source=gravity", 
"hippie-communes?pp=0", "history-of-the-vibrator", "history-of-the-vibrator?utm_campaign=whfbpd&utm_medium=social&utm_source=facebook", 
"homosexuality-norm", "hunger-games-facts?utm_campaign=6905&utm_medium=rpages&utm_source=Facebook&utm_term=1a9e42ac8abb6ffa90bf0542206505e74d3df12114a2c4445527fb2b88ef8880", 
"influential-photographs", "ingeniously-creative-ads", "insane-cults", 
"insane-rulers", "inspirational-quotes", "inspirational-quotes?utm_medium=referral&utm_source=taboolainternal", 
"interesting-facts-about-the-world", "interesting-quotes", "krokodil", 
"making-a-murderer-theories", "maya-angelou-greatest-quotes", 
"medieval-torture-devices", "milky-way-colorado", "montreal-metro", 
"most-popular-female-names-in-america", "neil-degrasse-tyson-tweets", 
"new-york-city-cinemagraphs", "new-york-subways-1980s", "north-korea-photographs", 
"north-korea-photographs?utm_campaign=taboolaINTL&utm_medium=referral&utm_source=taboola", 
"north-korea-photographs?utm_medium=referral&utm_source=taboolainternal", 
"obama-aging", "pablo-escobar", "pablo-escobar??utm_source=facebook", 
"pablo-escobar??utm_source=facebook&sr_source=lift_facebook&utm_campaign=simplereach_pablo-escobar&utm_medium=social", 
"pablo-escobar?utm_campaign=whfbpd&utm_medium=social&utm_source=facebook", 
"panda-facts", "photo-of-the-day-nasa-releases-crystal-clear-image-of-pluto", 
"pollution-in-china-photographs", "pollution-in-china-photographs?utm_campaign=3434&utm_medium=rpages&utm_source=Facebook&utm_term=1a0ddea7bed770d5473c45e9f8d81dfd0c4fdd232f207c6b88b53c41ff220c59", 
"pollution-in-china-photographs?utm_campaign=3434&utm_medium=rpages&utm_source=Facebook&utm_term=e28a76c1572c36c3a13965e52b4b2ea10518eb9f9c79c4bc84cfb85db16be81e", 
"pollution-in-china-photographs?utm_campaign=6806&utm_medium=rpages&utm_source=Facebook&utm_term=1a0ddea7bed770d5473c45e9f8d81dfd0c4fdd232f207c6b88b53c41ff220c59", 
"pollution-in-china-photographs?utm_campaign=7048&utm_medium=rpages&utm_source=Facebook&utm_term=2ef4bd7b6cd587601d6eeb35925282a1ed095ebbd4e9e4c0337ef868c7de7a0b", 
"pollution-in-china-photographs?utm_campaign=7458&utm_medium=rpages&utm_source=Facebook&utm_term=b9e79a51cd4daf4c3ec02accce75b3e1fc9a22cb3133460c9c32a4f2f9cdb68c", 
"powerful-photos-of-2014", "real-x-files", "romanovs-last-days", 
"science-of-human-decay", "scientific-discoveries-2015", "scully-effect", 
"serial-killer-quotes", "shah-iran", "six-of-the-craziest-gods-in-mythology", 
"space-facts", "sun-facts", "sunken-cities", "sunken-ships", 
"super-bowl-i-facts", "superhero-movies", "surreal-places", "syrian-civil-war-photographs", 
"the-five-greatest-mysteries-of-human-history", "the-four-most-important-battles-of-ancient-greece", 
"the-most-colorful-cities-in-the-world", "titanic-facts", "titanic-facts?utm_campaign=6385&utm_medium=rpages&utm_source=Facebook&utm_term=f5905e878216d14e20457ee3265caf6c10022d9545609edfb9a3cb0642c1a310", 
"titanic-facts?utm_campaign=6899&utm_medium=rpages&utm_source=Facebook&utm_term=b9e79a51cd4daf4c3ec02accce75b3e1fc9a22cb3133460c9c32a4f2f9cdb68c", 
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=106965c54919c24bf37356500ec50f0709b1de621d6950bb4c5d48759ea3677e", 
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=538659f1fc53f28d2c87b93ac73973681c1a46a04954964ab6c52ed1ab09b33a", 
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=91eae42c8fc9568103d46e0b6b6ec08fc34fd68b2e1918ffe2333ec73035c95a", 
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=ab594f0a1be002c8c3db297e8d33b04678af40e6a6469ac815884ae0a014b3a3", 
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=d1864657a05e5b716bb5cb16a29f068a55652eb39fb669ea9c22a6486198f227", 
"titanic-facts?utm_campaign=7292&utm_medium=rpages&utm_source=Facebook&utm_term=f5905e878216d14e20457ee3265caf6c10022d9545609edfb9a3cb0642c1a310", 
"us-veterans-portraits", "vintage-disneyland", "wall-street-early-20th-century", 
"what-we-love-this-week-the-incredible-last-words-of-famous-historical-figures", 
"woodstock-photos", "zombie-proof-house"), class = "factor"), 
    `0089` = c(0, 0, 0, 0, 0, 1), `0096` = c(0, 0, 0, 0, 0, 0
    ), `02` = c(0, 0, 0, 0, 0, 0), `0215` = c(0, 0, 0, 0, 0, 
    0), `0225` = c(0, 0, 0, 0, 0, 0), `0252` = c(0, 0, 0, 0, 
    0, 0), `0271` = c(0, 0, 0, 0, 0, 0), `0272` = c(0, 0, 0, 
    0, 0, 0), `03` = c(0, 0, 0, 0, 1, 1)), .Names = c("path", 
"0089", "0096", "02", "0215", "0225", "0252", "0271", "0272", 
"03"), row.names = c(NA, 6L), class = "data.frame")

我需要应用min(x,1)函数,以便此函数扫描数据帧中的每个值(第一列不是数字),并返回min(x,1)。那样我只有零和一个。 我试过了:

f <- function(x) min(1,x)
  res1<-do.call(f,tes[,2:ncol(tes)]) 

但是没有输出正确的结果。 任何帮助都得到了回报

1 个答案:

答案 0 :(得分:2)

我们可以使用pmin

tes[,-1] <- pmin(1, as.matrix(tes[,-1]))

或者,如果我们只需要二进制值

tes[,-1] <- +(!!tes[,-1])