Levels in R Dataframe

后端 未结 4 1301
囚心锁ツ
囚心锁ツ 2021-01-18 06:00

I imported data from a .csv file, and attached the dataset.
My problem: one variable is in integer form and has 295 levels. I need to use this variable to create others

4条回答
  •  长发绾君心
    2021-01-18 06:30

    Working from your clarification I suggest you redo your read statement with read.table and header=TRUE, stringAsFactors=FALSE and as.is = !stringsAsFactors and sep=",":

    datinp <- read.table("Rdata.csv", header=TRUE, stringAsFactors=FALSE , 
                           as.is = !stringsAsFactors , sep=",") 
    datinp$a <- as.numeric(datinp$a)
    datinp$b <- as.numeric(datinp$b)
    datinp$ctr <- with(datinp, as.integer(a/b) ) # no loop needed when using vector arithmetic
    

提交回复
热议问题