我有两个结构:
max_map <-
structure(list(name = structure(1:11, .Label = c("2-Acetylaminofluorene",
"amsacrine", "aniline", "aspartame", "cyclophosphamide", "doxorubicin",
"indomethacin", "phenacetin", "quercetin", "raloxifene", "urethane"
), class = "factor"), value = c(0.811811403850414, 0.8670680916324,
0.794704077953131, 0.652724115286456, 0.946812003911574, 0.94467294086402,
0.99210186168903, 0.965998352825426, 0.953645104970837, 0.903845608662668,
0.858610554863266)), .Names = c("name", "value"), row.names = c(NA,
-11L), class = "data.frame")
maps <-
structure(list(name = c("2-Acetylaminofluorene", "amsacrine",
"aniline", "aspartame", "cyclophosphamide", "doxorubicin", "indomethacin",
"phenacetin", "quercetin", "raloxifene", "urethane"), avg_relations_fan = c(0.596381660936706,
0.627169363301574, 0.52144016932515, 0.335756276148214, 0.710245148396949,
0.786168090022777, 0.931928694886563, 0.797790600434933, 0.836458734127729,
0.764397331494529, 0.548648356310039), baseline = c(0.441175818174093,
0.661376446637227, 0.470246408568704, 0.325159351267395, 0.664171399502648,
0.75247341151084, 0.894791275258052, 0.79447733086043, 0.791316894314006,
0.593161248492605, 0.546928771024265), baseline_mesh = c(0.511440934523423,
0.635334407445469, 0.466187120416127, 0.292197730456067, 0.712015987803737,
0.774493950979802, 0.936857915628513, 0.776404901563741, 0.786072875131457,
0.586564923115283, 0.602183350788001), standard = c(0.441269542443449,
0.656249151603696, 0.451995996997505, 0.331622681220588, 0.680778834932872,
0.742015626142688, 0.883911615393179, 0.791293422595675, 0.760673562009157,
0.559234401021581, 0.555385232882166), sum_relations_fan = c(0.593111715736251,
0.518197244570419, 0.52676186810563, 0.331234383858585, 0.697489423349489,
0.77249112456473, 0.940506641487552, 0.79946569580319, 0.82893149142568,
0.749819491774919, 0.624830313758535), total = c(0.593111715736251,
0.518197244570419, 0.52676186810563, 0.331234383858585, 0.697489423349489,
0.77249112456473, 0.940506641487552, 0.79946569580319, 0.82893149142568,
0.749819491774919, 0.624830313758535)), .Names = c("name", "avg_relations_fan",
"baseline", "baseline_mesh", "standard", "sum_relations_fan",
"total"), row.names = c(NA, 11L), class = c("cast_df", "data.frame"
), idvars = "name", rdimnames = list(structure(list(name = c("2-Acetylaminofluorene",
"amsacrine", "aniline", "aspartame", "cyclophosphamide", "doxorubicin",
"indomethacin", "phenacetin", "quercetin", "raloxifene", "urethane"
)), .Names = "name", row.names = c("2-Acetylaminofluorene", "amsacrine",
"aniline", "aspartame", "cyclophosphamide", "doxorubicin", "indomethacin",
"phenacetin", "quercetin", "raloxifene", "urethane"), class = "data.frame"),
structure(list(series = c("avg_relations_fan", "baseline",
"baseline_mesh", "standard", "sum_relations_fan", "total"
)), .Names = "series", row.names = c("avg_relations_fan",
"baseline", "baseline_mesh", "standard", "sum_relations_fan",
"total"), class = "data.frame")))
我想在x/y
数据框上应用函数maps
,
其中x是当前值,y是相应的值
name
。
我已经尝试了
mapply(function(x,y) {x/y}, t(maps[,!names(maps) %in% c('name')]), arrange(max_map, name)$value)
但是这给了我一个没有任何名字关联的大清单。我想要
结果与maps
数据帧类似,只是具有不同的值。
答案 0 :(得分:5)
我只是在这里猜测,但也许你正在寻找这样的事情:
m <- merge(maps,max_map)
m[,2:7] <- m[,2:7] / m[,8]
没有merge
且没有指定您拥有的列数:
maps[,-1] <- maps[,-1] / max_map$value
再次假设两者的顺序相同。
答案 1 :(得分:4)
Joran的答案肯定是更好的方法,但这可能有助于您更好地理解mapply
。每个参数都是一个列表,两者中较短的一个被回收,在本例中是第二个。
mapply(function(x,y) {x/y}, maps[,!names(maps) %in% c('name')], list(arrange(max_map, name)$value))
avg_relations_fan baseline baseline_mesh standard sum_relations_fan total
[1,] 0.7346308 0.5434462 0.6299997 0.5435616 0.7306028 0.7306028
[2,] 0.7233219 0.7627734 0.7327388 0.7568600 0.5976431 0.5976431
[3,] 0.6561438 0.5917252 0.5866172 0.5687601 0.6628403 0.6628403
[4,] 0.5143923 0.4981574 0.4476589 0.5080595 0.5074646 0.5074646
[5,] 0.7501438 0.7014818 0.7520141 0.7190222 0.7366715 0.7366715
[6,] 0.8322119 0.7965438 0.8198541 0.7854736 0.8177339 0.8177339
[7,] 0.9393478 0.9019147 0.9443163 0.8909484 0.9479940 0.9479940
[8,] 0.8258716 0.8224417 0.8037332 0.8191457 0.8276057 0.8276057
[9,] 0.8771174 0.8297813 0.8242824 0.7976485 0.8692243 0.8692243
[10,] 0.8457167 0.6562639 0.6489658 0.6187278 0.8295880 0.8295880
[11,] 0.6389956 0.6369928 0.7013463 0.6468418 0.7277226 0.7277226