-1

I want to create a data frame "Coefficients". At first I extract a column from another data frame "APD":

 str(APD.frame <- read.table("APD_data_15.txt", header = TRUE, sep = "", dec = "."))
APD.frame <- within(APD.frame, {
  Serial_number <- factor(Serial_number)
  Lot <- factor(Lot)
  Wafer <- factor(Wafer)} )
Coefficients<- data.frame(APD["Serial_number"])

Now I want to add some wildcard columns (makes the following procedure more easy to understand for me):

Coefficients[2]<- c(1)
Coefficients[3]<- 1
Coefficients[4]<- NA

In all cases I receive "unknown columns" (shown in RStudio by a mouse rollover). I need them to be numeric. Whyever I cannot convert them to numeric ones: Coefficients[3]<- as.numeric(Coefficients[3])

How can I ensure the columns to be numeric/why aren't they already?

Minimal example: http://www.datafilehost.com/d/5d00de23

Update:

> class(Coefficients[2])
[1] "data.frame"
> Coefficients<- transform(Coefficients, Coefficients[2] = as.numeric(Coefficients[2]))
Error: unexpected '=' in "Coefficients<- transform(Coefficients, Coefficients[2] ="
halfer
  • 19,824
  • 17
  • 99
  • 186
Ben
  • 1,432
  • 4
  • 20
  • 43
  • Please take some time to study `help("Extract")`. You probably mean to do `Coefficients[, 2]<- c(1)`. – Roland Oct 24 '17 at 12:30
  • @Roland It still remains "unknown". – Ben Oct 24 '17 at 12:32
  • When I add "Coefficients[, 2]<- NA" it is at least a boolean and not unknown. Though it does not help. – Ben Oct 24 '17 at 12:34
  • Please create a reproducible example, e.g., add the output of `dput(Coefficients)` to your question. There is no data type "unknown" in R. – Roland Oct 24 '17 at 12:38
  • An example is in the question already included. I'll add a screenshot of my issue. – Ben Oct 24 '17 at 12:40
  • Mh, the mouse rollover label vanishes by creating a screenshot.. what I mean is: When I move above the added column name then the following appears "column 2: unknown". In case of the first column there is "column 1: factor with 15 levels". – Ben Oct 24 '17 at 12:42
  • Possible duplicate of [How to convert a data frame column to numeric type?](https://stackoverflow.com/questions/2288485/how-to-convert-a-data-frame-column-to-numeric-type) – Manuel Bickel Oct 24 '17 at 12:47
  • "mouse rollover label"? There is no such thing in R. If you are using an IDE you need to give such details in the question. A link to a file hoster is not appreciated. Your question should contain all relevant information on this site. – Roland Oct 24 '17 at 12:47
  • RStudio.. You asked for a reproducible example and I told it is already there. All critic code lines are mentioned within the question. – Ben Oct 24 '17 at 12:50
  • See this [FAQ](https://stackoverflow.com/a/5963610/1412059) and this [Stack Overflow help page](https://stackoverflow.com/help/mcve). You definition of "reproducible" is wrong. – Roland Oct 24 '17 at 14:26
  • On the link there is described: " a minimal dataset, necessary to reproduce the error the minimal runnable code necessary to reproduce the error, which can be run on the given dataset. the necessary information on the used packages, R version, and system it is run on. in the case of random processes, a seed (set by set.seed()) for reproducibility" All that is already uploaded. What's the point? – Ben Oct 25 '17 at 07:30

1 Answers1

1

Your question is a duplicate, however, here the short version of an answer based on the detailed answer to this question How to convert a data frame column to numeric type?:

df <-  data.frame(X = c("1","2","3")
                  ,Y =c("3","4","5")
                 )

sum(df$X) # you`ll get an error
class(df$X)

df <- transform(df, X = as.numeric(df$X))

sum(df$X) # no more error, due to conversion to numeric
class(df$X)

#Update
#after discussion in chat the following lines helped

#convert all columns of data.frame to certain type, here numeric
df[] <- lapply(df, as.numeric)

#convert an individual column, to a certain type, here numeric
#check ?transform for accepted types for conversion
#df would be Coefficients in your example, 
#column name would be Serial_Number
df$columnname <- transform(df, columnname = as.numeric(df$columnname))
Manuel Bickel
  • 2,156
  • 2
  • 11
  • 22
  • Thanks, but I cannot apply that since I do not create X manually and therefore I get an error when transforming. I add it into the question. – Ben Oct 24 '17 at 12:48
  • Sure, my example is an abstract one, you simply need to exchange "X" by the name of the column you want to convert, e.g., "Coefficients". – Manuel Bickel Oct 24 '17 at 12:50
  • Ah, sorry the update has not popped up for me, yet, I`ll have a look... – Manuel Bickel Oct 24 '17 at 12:52
  • Ok, the thing is, entries in columns of a `data.frame` have to be of the same type. So the whole column "Coefficients" needs to be set to numeric. So simply skip all instances of [2] in your updated example. – Manuel Bickel Oct 24 '17 at 12:56
  • Does it work for you? I receive a different error: "> Coefficients<- transform(Coefficients, Coefficients = as.numeric(Coefficients)) Error in eval(substitute(list(...)), `_data`, parent.frame()) : (list) object cannot be coerced to type 'double'" – Ben Oct 24 '17 at 12:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157379/discussion-between-manuel-bickel-and-ben). – Manuel Bickel Oct 24 '17 at 13:08