gpt4 book ai didi

R- 在不同数据框中添加(添加)列的最佳方式

转载 作者:行者123 更新时间:2023-12-01 11:00:51 32 4
gpt4 key购买 nike

我有三个不同的数据框,如下所示:

V1.x<-c(1,2,3,4,5)
V2.x<-c(2,2,7,3,1)
V3.x<-c(2,4,3,2,9)
D1<-data.frame(ID=c("A","B","C","D","E"),V1.x=V1.x,V2.x=V2.x,V3.x=V3.x)

V1.y<-c(2,3,3,3,5)
V2.y<-c(1,2,3,3,5)
V3.y<-c(6,4,3,2,2)
D2<-data.frame(ID=c("A","B","C","D","E"),V1.y=V1.y,V2.y=V2.y,V3.y=V3.y)

V1<-c(3,2,4,4,5)
V2<-c(3,7,3,4,5)
V3<-c(5,4,3,6,3)
D3<-data.frame(ID=c("A","B","C","D","E"),V1=V1,V2=V2,V3=V3)

我想将所有 V1 列、所有 V2 列和所有 V3 列相加

V1_Add<-D1$V1.x+D2$V1.y+D3$V1
V2_Add<-D1$V2.x+D2$V2.y+D3$V2
V3_Add<-D1$V3.x+D2$V3.y+D3$V3

获取各个列的总和效果很好,但在实际数据中,列号来自 V1:V80,因此最好不必单独输入每个列。另外,我更希望得到一个包含所有最终总和的数据框,如下所示:

  ID  V1  V2  V3
1 A 6 6 13
2 B 7 11 12
3 C 10 13 9
4 D 11 10 10
5 E 15 11 14

最佳答案

library(reshape2)
library(plyr)

# First let's standardize column names after ID so they become V1 through Vx.
# I turned it into a function to make this easy to do for multiple data.frames
standardize_col_names <- function(df) {
# First column remains ID, then I name the remaining V1 through Vn-1
# (since first column is taken up by the ID)
names(df) <- c("ID", paste("V",1:(dim(df)[2]-1),sep=""))
return(df)
}

D1 <- standardize_col_names(D1)
D2 <- standardize_col_names(D2)
D3 <- standardize_col_names(D3)

# Next, we melt the data and bind them into the same data.frame
# See one example with melt(D1, id.vars=1). I just used rbind to combine those
melted_data <- rbind(melt(D1, id.vars=1), melt(D2, id.vars=1), melt(D3, id.vars=1))
# note that the above step can be folded into the function as well.
# Then you throw all the data.frames into a list and ldply through this function.

# Finally, we cast the data into what you need which is the sum of the columns
dcast(melted_data, ID~variable, sum)
ID V1 V2 V3
1 A 6 6 13
2 B 7 11 12
3 C 10 13 9
4 D 11 10 10
5 E 15 11 14



# Combined everything above more efficiently :

standardize_df <- function(df) {
names(df) <- c("ID", paste("V",1:(dim(df)[2]-1),sep=""))
return(melt(df, id.vars = 1))
}

all_my_data <- list(D1,D2,D3)
melted_data <- ldply(all_my_data, standardize_df)
summarized_data <- dcast(melted_data, ID~variable, sum)

关于R- 在不同数据框中添加(添加)列的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10941556/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com