I have a vector a<-c(0, 0). I want to convert this to a dataframe and then remove duplicated rows (as part of a loop).
This is my code:
a<-c(0, 0)
df<-t(as.data.frame(a))
distinct(df)
This isn't working because df isn't a dataframe even though I have converted it to a dataframe in the second step. I'm not sure how to make this work when the dataframe only has one row.
CodePudding user response:
swap t
and as.data.frame
like this:
library(dplyr)
a<-c(0, 0)
df<-as.data.frame(t(a))
distinct(df)
Output:
V1 V2
1 0 0
CodePudding user response:
The function t
will transform the data.frame back to a matrix. The simplest solution would be to simply change the order of the functions:
a <- c(0,0)
df <- as.data.frame(t(a))
distinct(df)
CodePudding user response:
You can use rbind.data.frame
function as follows:
a <- c(0,0)
df = rbind.data.frame(a)
distinct(df)
CodePudding user response:
Using dplyr
and matrix
:
library(dplyr)
data.frame(matrix(0, 1, 2)) %>%
distinct
#> X1 X2
#> 1 0 0