Dear colleagues,
I am trying to add rows to a data frame in R, but I don't want the column names to get modified. Here is the example, which'll help to explain the problem. Since the data is preloaded in R, I feel it should be easy to reproduce the example;
library(mlbench)
data("Soybean")
library(tidyverse)
var_unique <- data.frame(col_name=character(0),unique_cnt=integer(0))
col_names <- c(names(Soybean))
cnt <- 0
for (i in 2:5){
variable <- col_names[i]
cnt <- length(unique(Soybean[,variable]))
#print (cnt)
vec <- c(variable, cnt)
#print(vec)
var_unique <- rbind(var_unique,vec)
}
As you can see I'm trying to make a data frame with count of unique values for the variable. Can I get help with two questions;
- First, when the loop executes, I get the answer as following;
> var_unique
X.date. X.8.
1 date 8
2 plant.stand 3
3 precip 4
4 temp 4
I am not very sure why did the column names get renamed. Is it possible to keep the column names as col_name
and unique_cnt
?
- Second, earlier I defined the column
unique_cnt
as numeric data type, but since we're coercing it into a vectorvec
, the final data type turns out to be character. Is it possible to keep the datatype as it is, when the data frame is defined in the first place.
Help is greatly appreciated. Thanks in advance.