将某些列中的数值除以一列的值,并替换数据表中的内容。

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

Divide values in some columns by the value of a column and replace in data table

问题

使用R,我想要将数据表中第21列到第150列的值除以第5列的值,并替换第21列到第150列的值。例如,第21列将被第5列的值除以,然后第21列中的新值将被商数替代。第22列也会被第5列的值除以,然后第22列的值将被更新为除法的结果。

首先,我尝试了以下方法,但它没有替换列中的值,而且输出中没有1到20列:

df1000 <- df1000[,21:150] / df1000[,5]

然后,我尝试使用dplyr,但出现了一些问题:

df1000 %>% mutate_at(seq(21:150),funs(df[,21:150]/df[,5]))

因为我的示例数据非常大,有150列,所以我们可以使用内置数据集,比如iris,然后我可以调整列的范围吗?

英文:

Using R, I want to divide the values in columns 21 through 150 in my data table by the value in column 5 and replace the value that is in columns 21 through 150 with the new divided value. So for example column 21 would be divided by column 5 and the new value in column 21 is then replaced by the quotient. The same for column 22 gets divided by column 5 and then the column 22 value is updated with the divided value.

First I tried this but it does not replace the values in the columns and then output does not have columns 1-20...

df1000 &lt;- df1000[,21:150] / df1000[,5]

so then I tried dplyr but something is wrong

df1000 %&gt;% mutate_at(seq(21:150),funs(df[,21:150]/df[,5]))

Since my sample is so large with 150 columns instead of providing sample data can we use a built-in dataset like iris and I can adjust the column range?

答案1

得分: 2

你的第一次尝试非常接近。只需要将新数据与相同的索引存储回去。

df1000[,21:150] <- df1000[,21:150] / df1000[,5]
英文:

You were really close with your first attempt. Just needed to store the new data back with the same indeces.

df1000[,21:150] &lt;- df1000[,21:150] / df1000[,5]

答案2

得分: 0

在dplyr中,您应该使用across语法,因为dplyr中的_at/_all/_if函数已被弃用:

# 您可以像以前一样使用管道 %>%,我只是更喜欢使用一行
# 使用across()来标识列
# 使用~引入要应用的函数
# 使用.x来引用输入列
dplyr::mutate(df1000, dplyr::across(21:150, ~ .x / df1000[,5]))

# 或者您可以直接使用列名(我将第五列命名为fifth_column)
dplyr::mutate(df1000, dplyr::across(21:150, ~ .x / fifth_column))
英文:

in dplyr you should use the across synthax since the _at/_all/_if functions are suspended in dplyr:

# you can pipe %&gt;% just like before, I just prefer to use one line
# use the across() to identify columns and 
# use ~ to intoduce the function to be applied
# use .x to refer to input columns
dplyr::mutate(df1000, dplyr::across(21:150, ~ .x / df1000[,5]))

# or you can use de column name directly (I called the fifth
dplyr::mutate(df1000, dplyr::across(21:150, ~ .x / fifth_column))

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

发表评论

匿名网友

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

确定