如何在数据帧的同一行中插入3个向量?

时间:2022-02-04 04:27:14

I'm new in R and I have a problem. I created an "empty" data.frame, as in R I cannot do this, I created a vector to fulfil it that I would delete when finishing the script.

我是R的新人,我有一个问题。我创建了一个“空”data.frame,因为在R中我不能这样做,我创建了一个向量来实现它,我将在完成脚本时删除它。

x <- c("aa", 0, 0, 0, "zz") #Vector to fulfill the dataframe (it will be deleted at the end of the script)       
df <<- rbind(x) #Creating a matrix. See: class(df)    
df <<- data.frame(df) #Converting the matrix into a data.frame 

But now I need to fulfil that dataframe with three vectors:

但现在我需要用三个向量来实现该数据帧:

a <- c("bb")       
b <- c(2, 3, 4)    
c <- c("yy")              

The desired output is a dataframe like this:

所需的输出是这样的数据帧:

    X1  X2  X3  X4  X5

r1  aa  0   0   0   zz

r2  bb  2   3   4   yy

I have tried this: df <- rbind(df, a, b, c) but it does not work...

我试过这个:df < - rbind(df,a,b,c)但它不起作用......

Any suggestion?

有什么建议吗?

2 个解决方案

#1


2  

Given your desired output you may try this:

library(dplyr)  # Gives %>%
dataF <- data.frame(X1 = "aa",
                    X2 = 0,
                    X3 = 0,
                    X4 = 0,
                    X5 = "zz",
                    stringsAsFactors = FALSE)

a <- c("bb")       
b <- c(2, 3, 4)    
c <- c("yy")

# setNames to simply use rbind()
newrow <- data.frame(a, t(b), c, stringsAsFactors = FALSE) %>% setNames(names(dataF))

rbind(dataF, newrow)

Which gives you:

哪个给你:

'data.frame':   2 obs. of  5 variables:
 $ X1: chr  "aa" "bb"
 $ X2: num  0 2
 $ X3: num  0 3
 $ X4: num  0 4
 $ X5: chr  "zz" "yy"

And guessing your even more desired output:

library(dplyr)
dataF <- data.frame(X1 = character(),
                    X2 = numeric(),
                    X3 = numeric(),
                    X4 = numeric(),
                    X5 = character(),
                    stringsAsFactors = FALSE)

a <- c("bb")       
b <- c(2, 3, 4)    
c <- c("yy")

newrow <- data.frame(a, t(b), c, stringsAsFactors = FALSE) %>% setNames(names(dataF))

rbind(dataF, newrow)

Which gives you:

哪个给你:

'data.frame':   1 obs. of  5 variables:
 $ X1: chr "bb"
 $ X2: num 2
 $ X3: num 3
 $ X4: num 4
 $ X5: chr "yy"

#2


0  

Maybe, you can just do

也许,你可以做到

x <- c("aa", 0, 0, 0, "zz") 
a <- c("bb")       
b <- c(2, 3, 4)    
c <- c("yy")   
y <- c(a, b, c)
rbind(r1 = x,r2 =  y)


#   [,1] [,2] [,3] [,4] [,5]
#r1 "aa" "0"  "0"  "0"  "zz"
#r2 "bb" "2"  "3"  "4"  "yy"

This is a matrix, to get it as a data frame, you can

这是一个矩阵,要把它作为数据框,你可以

data.frame(rbind(r1 = x,r2 =  y))

#   X1 X2 X3 X4 X5
#r1 aa  0  0  0 zz
#r2 bb  2  3  4 yy

#1


2  

Given your desired output you may try this:

library(dplyr)  # Gives %>%
dataF <- data.frame(X1 = "aa",
                    X2 = 0,
                    X3 = 0,
                    X4 = 0,
                    X5 = "zz",
                    stringsAsFactors = FALSE)

a <- c("bb")       
b <- c(2, 3, 4)    
c <- c("yy")

# setNames to simply use rbind()
newrow <- data.frame(a, t(b), c, stringsAsFactors = FALSE) %>% setNames(names(dataF))

rbind(dataF, newrow)

Which gives you:

哪个给你:

'data.frame':   2 obs. of  5 variables:
 $ X1: chr  "aa" "bb"
 $ X2: num  0 2
 $ X3: num  0 3
 $ X4: num  0 4
 $ X5: chr  "zz" "yy"

And guessing your even more desired output:

library(dplyr)
dataF <- data.frame(X1 = character(),
                    X2 = numeric(),
                    X3 = numeric(),
                    X4 = numeric(),
                    X5 = character(),
                    stringsAsFactors = FALSE)

a <- c("bb")       
b <- c(2, 3, 4)    
c <- c("yy")

newrow <- data.frame(a, t(b), c, stringsAsFactors = FALSE) %>% setNames(names(dataF))

rbind(dataF, newrow)

Which gives you:

哪个给你:

'data.frame':   1 obs. of  5 variables:
 $ X1: chr "bb"
 $ X2: num 2
 $ X3: num 3
 $ X4: num 4
 $ X5: chr "yy"

#2


0  

Maybe, you can just do

也许,你可以做到

x <- c("aa", 0, 0, 0, "zz") 
a <- c("bb")       
b <- c(2, 3, 4)    
c <- c("yy")   
y <- c(a, b, c)
rbind(r1 = x,r2 =  y)


#   [,1] [,2] [,3] [,4] [,5]
#r1 "aa" "0"  "0"  "0"  "zz"
#r2 "bb" "2"  "3"  "4"  "yy"

This is a matrix, to get it as a data frame, you can

这是一个矩阵,要把它作为数据框,你可以

data.frame(rbind(r1 = x,r2 =  y))

#   X1 X2 X3 X4 X5
#r1 aa  0  0  0 zz
#r2 bb  2  3  4 yy