Unable to designate CSV column heads "as.factor" for R -Error
1
1
Entering edit mode
6.1 years ago
wkmustahs21 ▴ 30

Hi

I am having an issue with assigning factors to my data CSV. Here is a summary of the data frame:

data.frame': 303 obs. of 12 variables:

PLOT : int 19 177 54 114 41 48 142 134 160 267 ...

RANGE : int 2 12 4 8 3 4 10 9 11 18 ...

ROW : int 4 12 9 9 11 3 7 14 10 12 ...

REP : int 1 1 1 1 1 1 1 1 1 1 ...

ENTRY : Factor w/ 184 levels "","17_YMG_0293",..: 40 40 77 82 87 88 102 103 103 6 ...

PLOT_ID : Factor w/ 301 levels "","18_HZG_OvOv_001",..: 20 178 55 115 42 49 143 135 161 268 ...

Shatter : num 9 9 9 9 9 9 9 9 9 8 ...

Chaff.Color : Factor w/ 4 levels "","*Blank ones are segregating in color",..: 3 4 3 4 4 4 3 4 4 3 ...

Heading_d.from.Jan.1: int 138 139 137 133 135 135 133 137 135 136 ...

Height_cm : int 74 73 77 76 74 79 78 73 76 70 ...

Plot.weight..kg. : num 0.26 0.18 0.19 0.14 0.33 0.19 0.13 0.11 0.24 0.18 ...

But I get this error:

HAYSData$Rep<-as.factor(HAYSData$Rep)

Error in $<-.data.frame(*tmp*, Rep, value = integer(0)) :

replacement has 0 rows, data has 303

I get the same type of error for Entry, Range, and Rows. I am not sure when I look at length(Entry) for example I get 300. I even tested with changing "as.factor" to "as.numeric" but it does not help.

I don't have an "na" in my data each category is its own column as well.

I don't know if something is wrong with my CSV. I have worked this same script with another CSV but no issues in the part of the script for the other data.

Can someone please help me?

R • 1.3k views
ADD COMMENT
1
Entering edit mode

Can you show the entire output of, for example, HAYSData$Rep and as.factor(HAYSData$Rep) ? Paste them here and wrap with the code button

ADD REPLY
0
Entering edit mode

Hi

So I did it two ways:

HAYSData$REP

[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >1 1 1 1 1 1 1

[55] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >1 1 1 1 1 1 1 1

[109] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1

[163] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1

[217] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1

[271] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 NA NA NA

then I wrote it as in the script:

HAYSData$Rep

NULL

This is what I have when I write "as.factor(HAYSData$REP)"

[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[33] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[65] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[97] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 .....[289] 1 1 1 1 1 1 1 1 1 1 1 1 <na> <na> <na> Levels: 1

For just "as.factor(HAYSData$Rep) :

as.factor(HAYSData$Rep)

factor(0)

One thing I notice is that my headers in my CSV are in Caps Lock ...so could this be a reason of why R is not recognizing my headers correctly?

ADD REPLY
1
Entering edit mode

Yes, R is case sensitive. That looks like the issue.

ADD REPLY
0
Entering edit mode
6.1 years ago
jean.elbers ★ 1.7k

Try

HAYSData$Rep<-as.factor(paste(HAYSData$Rep))
ADD COMMENT

Login before adding your answer.

Traffic: 2533 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6