如何使用R dplyr获得多个列值的平均值
(我是一名SAS程序员,试图学习R) 我有多个数字列。使用如何使用R dplyr获得多个列值的平均值,r,dplyr,mean,R,Dplyr,Mean,(我是一名SAS程序员,试图学习R) 我有多个数字列。使用dplyr,我想得到每行多个值的平均值。在SAS中,这将是: newvariable = mean(of x1, x2, x3, x4, x5); 我想象它在R中是这样的,但它只是生成了一条错误消息 代码: 库(dplyr) 图书馆(tidyr) #如果尚未转换为数据帧,则转换为数据帧。 数据帧什么错误消息?你也能提供一份你的数据样本吗?您可以通过键入dput(head(data))并将输出添加到您的问题中来实现这一点。因此of在RTr
dplyr
,我想得到每行多个值的平均值。在SAS中,这将是:
newvariable = mean(of x1, x2, x3, x4, x5);
我想象它在R中是这样的,但它只是生成了一条错误消息
代码:
库(dplyr)
图书馆(tidyr)
#如果尚未转换为数据帧,则转换为数据帧。
数据帧什么错误消息?你也能提供一份你的数据样本吗?您可以通过键入dput(head(data))
并将输出添加到您的问题中来实现这一点。因此of
在RTrytime1data%>%mutate(newvariable=rowMeans([c(“x1”、“x2”、“x3”、“x4”、“x5”))
基本R解决方案可能类似于:rowMeans(time1data[,c(“x1”、“x2”、“x3”、“x4”、“x5”))
谢谢,这起作用了:time1data$newvarI我建议对
使用命名约定,以符合tidyverse
原则。谢谢大家。这是我第一次在这里问问题。我真的很感谢你的回复。我无法理解一些回复,也不知道如何回复。对不起,谢谢你!
time1data %>%
mutate(newvariable = mean(of x1, x2, x3, x4, x5)) %>%
head
library(dplyr)
library(tidyr)
# convert to a data frame if not already.
data_frame <- as.data.frame(your_data)
# These are the columns I used for testing for data with five columns
# and are to help you understand the next line of code.
colnames(dataframe) <- c("First", "Second", "Third", "Forth", "Fifth")
# tidyverse gather() via install.packages("tidyverse")
# index is the name of the "key" column, "Group_Mean" of value column
# Kept the convert = True otherwise, they become factor or strings
df.gather.col <- gather(data_frame, key="index", value="Group_Mean",
First, Second, Third, Forth, Fifth,
convert=True)
#just prints it out.
df.gather.col