将 log 应用到所有大于 0 的数字(具有两个 .cols 条件的 across())
我正在尝试在数据框中创建多个变量的日志,其中还包括非数字变量,并且希望仅将该函数应用于那些不包含零或负值的数字变量。
这就是我所处的位置:
# creating a df with numeric and factor variables
a <- c(3, -1, 0, 5, 2)
b <- c(1, 3, 2, 1, 4)
c <- c(9, -2, 3, -5, 1)
d <- c(3, 0, 6, 1, 5)
e <- c("red", "blu", "yellow", "green", "white")
f <- c(0, 1, 1, 0, 0)
g <- c(3, 1, 1, 4, 2)
df <- data.frame(a,b,c,d,e,f,g) %>%
mutate_at("f",factor)
#applying the transformation to all numeric variables
df.log <- df %>%
as_tibble() %>%
mutate(across(
.cols = is.numeric, #& all()>0,#ideally I shall add here the condition '& >0' but it doesn't work
.fns = list(log = log),
.names = "{.col}_{.fn}"))
使用上面的代码,我用 NaN
表示负值,用 -inf
表示零。然后我可以删除包含这些值的列,但我想找到一种干净的方法来一次性完成这一切。
另一个想法是删除之前值为 <=0
的列,如下所示:
df.skim <- df %>%
select_if(is.numeric)
df.skim <- df.skim[,sapply(df.skim, min)>0]
然后将日志应用到左侧的列,但通过这种方式,我也删除了关键列,并且无法轻松合并回数据。
I am trying to create the log of multiple variables in a dataframe which includes also non numeric variables, and would like to apply the function only to those numeric variables which include no zeros or negative values.
This is where I am at:
# creating a df with numeric and factor variables
a <- c(3, -1, 0, 5, 2)
b <- c(1, 3, 2, 1, 4)
c <- c(9, -2, 3, -5, 1)
d <- c(3, 0, 6, 1, 5)
e <- c("red", "blu", "yellow", "green", "white")
f <- c(0, 1, 1, 0, 0)
g <- c(3, 1, 1, 4, 2)
df <- data.frame(a,b,c,d,e,f,g) %>%
mutate_at("f",factor)
#applying the transformation to all numeric variables
df.log <- df %>%
as_tibble() %>%
mutate(across(
.cols = is.numeric, #& all()>0,#ideally I shall add here the condition '& >0' but it doesn't work
.fns = list(log = log),
.names = "{.col}_{.fn}"))
With the code above I have NaN
for negative values and -inf
for zeros. I could then drop columns with those values, but I'd like to find a clean way to do it all at once.
Another idea was to remove columns with values <=0
before as follows:
df.skim <- df %>%
select_if(is.numeric)
df.skim <- df.skim[,sapply(df.skim, min)>0]
and then apply the log to the columns left, but in this way I drop also the key column and I cannot easily merge back the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个小函数,然后将其传递到
where
内的across
:您可以像这样使用它:
由 reprex 包 (v2.0.1)
You can create a little function that you then pass onto
across
insidewhere
:Which you use like this:
Created on 2022-03-10 by the reprex package (v2.0.1)
您可以将
where()
与匿名函数一起使用来指定更复杂的条件,例如您的条件:输出:
You can use
where()
with an anonymous function to specify more complex conditions like yours:Output: