Rstudio功能

huangapple go评论77阅读模式
英文:

Rstudio Functions

问题

我是相对新手的Rstudio用户,正在尝试编写一个函数,其中我要检查来自“Book 1”单元格行中的字符串是否与来自不同Excel表“Book2”中的单元格行中的字符串匹配,如果它们不匹配,则从“Book1”中删除该行。我认为我的问题在于使用"!="来检查字符串中的内容,但我不确定接下来该怎么办。

到目前为止,我的代码如下:

for (i in Book1[,1]) { 
     if i != Book2[,1] { 
          Book1[-c(i)]
     }
}

我原本期望的输出是从“Book1”中删除与“Book2”不匹配的行。

英文:

I am fairly new to Rstudio and am trying to write a function where I check if the string in a row of cells from Book 1 match the string inside a row of cells from a different excel sheet, Book2 and if they do not delete the row from Book1. I think my issue is using != to check what is inside the string but I'm unsure where to go from here.

This is what I have so far

for (i in Book1[,1] 
     { if i != Book2[,1] { 
          Book1[-c(i)] }}

I was expecting my output to remove the rows from Book1 that are not in Book2.

答案1

得分: 0

这里有很多需要解释的地方。首先,您没有使用函数而是使用了循环,这是可以的,只是术语不同。其次,在R中,括号索引是[行,列],因此您正在索引整列,而您声称要查找行。我认为您想要匹配每个单元格中的值,这需要双重索引,如我下面所示。另外,您应该包含一些数据,以便这里的人们可以重现您的实际问题。我已经编造了一些数据以便开始。希望这有所帮助。

book1 <- data.frame(x=round(rnorm(10,5,1)),
                 y=round(rnorm(10,10,2)))
book2 <- data.frame(x=round(rnorm(10,5,1)),
                 y=round(rnorm(10,10,2)))
book1
book2

for (i in 1:10) { # 请注意这是行数...1 - 10
  for (j in 1:2) {# 请注意这是列数...1 - 2
    if(book2[i,j]!=book1[i,j]) { # 如果book2中第i行第j列的值不等于book1中第i行第j列的值
      book1[i,j] <- NA # 将book1中第i行第j列的值设置为NA
    }
  }
}

book1 # 查看结果
英文:

A lot to unpack here. First you haven't used a function but a loop, which is fine just terminology. Second, in R bracket indexing is [row,column] so you are indexing an entire colum where you state you are looking for rows. I think you are looking to match the value in each cell which requires double indexing as I've shown below. Also you should include some data so that the folks on here can reproduce your actual problem. I have made up some to get started. Hopefully this helps.

book1 &lt;- data.frame(x=round(rnorm(10,5,1)),
                 y=round(rnorm(10,10,2)))
book2 &lt;- data.frame(x=round(rnorm(10,5,1)),
                 y=round(rnorm(10,10,2)))
book1
book2


for (i in 1:10) { # Note this is the number of rows...1 - 10
  for (j in 1:2) {# Notre this is the number of columns...1 - 2
    if(book2[i,j]!=book1[i,j]) { # if the value in book2 row i column j is not equal to the value in book1 row i column j
      book1[i,j] &lt;- NA # set book1 row i column j equal to NA
    }
  }
}

book1 # view the result

答案2

得分: 0

以下是翻译好的部分:


# 获取要用作参数的变量名称列表
varx <- names(book1)

df <- function(df1, df2, x){
  vars <- x
  vars <- unlist(strsplit(vars, split = ','))
  for(i in vars){
    df1[[i]][which(df1[[i]] != df2[[i]])] <- NA
    df <- df1
  }
  return(df)
}

dfx <- df(book1, book2, varx)

dfx 

创建于2023年07月03日,使用 reprex v2.0.2

英文:

Alternatively you may try the below function as well


# get the list of variable names to use it as a argument
varx &lt;- names(book1)

df &lt;- function(df1,df2,x){
  vars &lt;- x
  vars &lt;- unlist(strsplit(vars,split = &#39;,&#39;))
  for(i in vars){
  df1[[i]][which(df1[[i]]!=df2[[i]])] &lt;- NA
  df &lt;- df1
  }
  return(df)
}

dfx &lt;- df(book1,book2,varx)

dfx 

<sup>Created on 2023-07-03 with reprex v2.0.2</sup>

答案3

得分: 0

以下是翻译好的部分:

library(tidyverse)

book1 %>% names() %>% map(\(x) {
  book1[[x]][which(book1[[x]]!=book2[[x]])] <-  NA
  return(book1[[x]])
  }) %>% setNames(.,names(book1)) %>% as.data.frame()

创建于2023年7月3日,使用reprex v2.0.2

英文:

Alternatively with map function

library(tidyverse)

book1 %&gt;% names() %&gt;% map(\(x) {
  book1[[x]][which(book1[[x]]!=book2[[x]])] &lt;-  NA
  return(book1[[x]])
  }) %&gt;% setNames(.,names(book1)) %&gt;% as.data.frame()

<sup>Created on 2023-07-03 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月3日 22:00:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定