将所有列除以特定列,除了一个
例如:我有4列的框架,我希望将列A和b划分为C,但是我希望
A B C ID
4 8 23 1
5 12 325 2
6 23 56 3
73 234 21 4
23 23 213 5
我期望的结果
A B C ID
0,173913043 0,347826087 1 1
0,015384615 0,036923077 1 2
0,107142857 0,410714286 1 3
3,476190476 11,14285714 1 4
0,107981221 0,107981221 1 5
不变的列ID或没有C列C,
我有代码给出的代码我只在没有列“ ID”的情况下A和B列
columns_to_divide <- c(1,2)
results <- results[,columns_to_divide ]/results[,3]
For example : I have frame with 4 columns and I want divide columns A and B by C, but I want unchanged column ID
A B C ID
4 8 23 1
5 12 325 2
6 23 56 3
73 234 21 4
23 23 213 5
The result which i expect is
A B C ID
0,173913043 0,347826087 1 1
0,015384615 0,036923077 1 2
0,107142857 0,410714286 1 3
3,476190476 11,14285714 1 4
0,107981221 0,107981221 1 5
or without the column C, doesn't matter
So, I have the code which give me only columns A and B without the column 'ID'
columns_to_divide <- c(1,2)
results <- results[,columns_to_divide ]/results[,3]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们可以使用
突变
,该可以在列中创建或改变值。 说要更改A和B列,然后我们可以定义一个函数,以将这两个列除以C。We can use
mutate
, which creates or alters the values in a column.across
says to alter columns A and B, and then we can define a function to divide both of these columns by C.数据
Data
创建列
添加到数据框架
划分并打印
Create Columns
Add to data frame
divide by and print