我正在尝试编写一个函数,将R

时间:2020-03-04 04:52:04

标签: r

它应该做的事情是像w = c(w1,w2,w3)这样的向量,它要做的是w1*w2w1*w3w2*w3。我正在寻找一个通用的公式,足以承受10倍或更多的重量。

multelem <- function(x){
+     x1 <- numeric(0)
+     for(i in x){
+         x1 <- x[i]*x[i+1]  
+     }
+     print(x1)
+ }

这个想法是将多个权重彼此相邻。如果此功能有效,我将添加一行x2 <- x[i]*x[i+2],直到length(w)。不幸的是,我无法很好地完成此功能。我认为我有直觉的权利,但执行不起作用。请分享任何想法,或者如果已经有解决方案,请告诉我。

预先感谢

3 个答案:

答案 0 :(得分:2)

沿着latemail答案行。

x=c(1,2,3,4)
combn(x, 2, FUN = function(x) x[1]*x[2]) 

答案 1 :(得分:0)

请尝试以下操作:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

由于您使用的是(x中的i),因此它以向量中的第一个数字为i,因此找不到x [i + 1]的数字。

答案 2 :(得分:0)

您可以尝试一下

const array = [
{      
  articles: {
    id: "1",
    title: "Great food article",
    featured: false,
    paid: false,
    image: "image",
    author: "Chocolate Boy",
    date:"1-2-2020",
    description:
      "Street art edison bulb gluten-free, tofu try-hard brooklyn tattooed pickled chambray. Actually humblebrag next level, deep v art party wolf tofu direct trade readymade sustainable hell of banjo. Organic authentic subway tile cliche palo santo, street art XOXO dreamcatcher retro sriracha portland air plant kitsch stumptown. Austin small batch squid gastropub. Pabst pug tumblr gochujang offal retro cloud bread bushwick semiotics before they sold out sartorial literally mlkshk. Vaporware hashtag vice, sartorial before they sold out pok pok health goth trust fund cray.",
    tags: [
      "Tech",
      "Food"
    ]
  }
},
{      
    articles: {
      id: "2",
      title: "Good tech article",
      featured: false,
      paid: false,
      image: "iamge",
      author: "Candy Boy",
      date:"1-2-2020",
      description:
        "Street art edison bulb gluten-free, tofu try-hard brooklyn tattooed pickled chambray. Actually humblebrag next level, deep v art party wolf tofu direct trade readymade sustainable hell of banjo. Organic authentic subway tile cliche palo santo, street art XOXO dreamcatcher retro sriracha portland air plant kitsch stumptown. Austin small batch squid gastropub. Pabst pug tumblr gochujang offal retro cloud bread bushwick semiotics before they sold out sartorial literally mlkshk. Vaporware hashtag vice, sartorial before they sold out pok pok health goth trust fund cray.",
      tags: [
        "Tech",
        "Adventure"
      ]
    }
  },
  {      
    articles: {
      id: "3",
      title: "Nice adventure article",
      featured: false,
      paid: false,
      image: "image",
      author: "Lollypop Boy",
      date:"1-2-2020",
      description:
        "Street art edison bulb gluten-free, tofu try-hard brooklyn tattooed pickled chambray. Actually humblebrag next level, deep v art party wolf tofu direct trade readymade sustainable hell of banjo. Organic authentic subway tile cliche palo santo, street art XOXO dreamcatcher retro sriracha portland air plant kitsch stumptown. Austin small batch squid gastropub. Pabst pug tumblr gochujang offal retro cloud bread bushwick semiotics before they sold out sartorial literally mlkshk. Vaporware hashtag vice, sartorial before they sold out pok pok health goth trust fund cray.",
      tags: [
        "Tech",
        "Food",
        "Adventure"
      ]
    }
  },
];

const filteredArray = array.map(item => item.articles.tags.includes("Food") && item);

console.log(filteredArray);
相关问题