重新编码多列以创建新的df列

时间:2017-12-13 23:30:19

标签: r lapply recode mutate

我正在尝试编写一个函数,它会改变df中的多个列,并为每个重新编码的变量生成一个新列。在这种情况下,我正在运行的突变是从15中减去列中的每个元素。我能够为三列编写以下代码,这有效,但是在将来我想运行20多列这样的内容写出每个新的列名称(就像你在mutate中所做的那样)似乎很麻烦 我似乎不能使用lapply来使用recode或mutate函数来生成新列。

df2 <- mutate(df1, new_col1 = 15-old_col1, 
         new_col2 = 15 - old_col2, new_col3 = 15 - old_col3)

1 个答案:

答案 0 :(得分:1)

lastClick = []; var killersGifs = { killerSearches: ["Freddy Krueger", "Jason Voorhees", "Pennywise", "Ghostface", "American Mary", "Chucky", "Bride of Chucky", "The Candyman", "Cujo", "Hannibal", "Leatherface", "Michael Myers", "Norman Bates", "Pinhead"], buttonLoop: function() { for (var b = 0; b < killersGifs.killerSearches.length - 1; b++) { var buttonM = $("<button class='dynGen'>").text(killersGifs.killerSearches[b]).attr("data-index", killersGifs.killerSearches[b]); $("#buttons").append(buttonM); } }, divLoop: function(click) { var queryURL = "https://api.giphy.com/v1/gifs/search?api_key=B26sstJns2pZuNT5HiJpqS5FV8Su1sDd&q=" + lastClick + "&limit=10" $.ajax({ url: queryURL, method: "GET" }).done(function(response) { console.log(response.data); for (var i = 0; i < response.data.length; i++) { var respData = response.data[i]; var image = respData.images.fixed_height_still.url; var gif = respData.images.fixed_height.url; var rating = respData.rating; var dynDiv = $("<div class='dyn-div'>"); //dynDiv.attr("data-index", i); var killerImg = $("<img class='still-image'>"); killerImg.attr("src", image); killerImg.attr("alt", "Serial Killer still frame of gif"); killerImg.attr("data-gif", gif); killerImg.attr("class", "killerImg"); killerImg.attr("data-index", i); killerImg.attr("data-img", image); dynDiv.append("<p> Rating: " + rating + "</p>"); dynDiv.append(killerImg); $("#append-img-div").prepend($(dynDiv)); }; }); }, userPush: function () { var userInput = $("input[type='text']").val().trim(); killersGifs.killerSearches.push(userInput); var buttonU = $("<button class='dynGen'>").text(userInput).attr("data-index", userInput); $("#buttons").append(buttonU); console.log(killersGifs.killerSearches); } }; killersGifs.buttonLoop(); $("#killer-add-submit").on("click", function(event) { event.preventDefault(); killersGifs.userPush(); }); $(document).on("click", "button.dynGen", function(event) { var currentIndex = $(this).attr("data-index"); lastClick.push(currentIndex); console.log(currentIndex); event.preventDefault(); $("#append-img-div").empty(); killersGifs.divLoop(); lastClick = []; }); $(document).on("click", ".killerImg", function(event) { console.log("test"); //killersGifs.animateGif(); var currentIn = $(this).attr("data-index"); var tempUrl = $(this).attr("data-gif"); var tempUrl2 = $(this).attr("data-img"); console.log(currentIn); console.log(tempUrl); if ($(this).attr("src") == tempUrl2) { $(this).attr("src", tempUrl); } else if ($(this).attr("src") == tempUrl) { $(this).attr("src", tempUrl2); }; }); 解决方案,假设您要改变所有列*(请参阅下面的更灵活版本)。

*正如@ sb0709在评论中提到的那样,data.table也会这样做。

mutate_all

给出了:

library( data.table )
df <- data.table( old_col_1 = 20:24,
                  old_col_2 = 55:49,
              old_col_3 = rnorm( 5, 100, 30 ) )

df[ , sub( "old", "new", names( df ) ) := lapply( .SD, function(x) 15-x ) ]

如果要选择少于所有列,则只需要对R> df old_col_1 old_col_2 old_col_3 new_col_1 new_col_2 new_col_3 1: 20 55 86.29104 -5 -40 -71.29104 2: 21 56 144.21564 -6 -41 -129.21564 3: 22 57 104.84574 -7 -42 -89.84574 4: 23 58 93.18084 -8 -43 -78.18084 5: 24 59 104.96188 -9 -44 -89.96188 向量和names列表进行子集化。例如,仅在第2列和第3列上运行变异:

.SD

而是给出了:

df[ , sub( "old", "new", names( df )[2:3] ) := lapply( .SD[,2:3], function(x) 15-x ) ]