2017-10-24 1 views
-1

데이터 프레임 "계수"를 만들고 싶습니다.열이 숫자인지 확인하십시오/변환이 작동하지 않습니다.

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

에서 :

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"]) 

가 지금은 (나를 위해 이해하기 더 쉬운 다음 절차를 만드는) 일부 와일드 카드 열을 추가 할 : 처음에는 다른 데이터 프레임 "APD"에서 열을 추출 모든 경우에 "알 수없는 열"(마우스 롤오버로 RStudio에 표시됨)이 나타납니다. 나는 그것들을 숫자로 나타내야한다. 숫자를 숫자로 변환 할 수없는 이유는 무엇입니까? Coefficients[3]<- as.numeric(Coefficients[3])

열을 숫자로 표시하려면 어떻게해야합니까?

최소 예 : http://www.datafilehost.com/d/5d00de23

업데이트 :

> class(Coefficients[2]) 
[1] "data.frame" 
> Coefficients<- transform(Coefficients, Coefficients[2] = as.numeric(Coefficients[2])) 
Error: unexpected '=' in "Coefficients<- transform(Coefficients, Coefficients[2] =" 
+0

'help ("Extract")'에 대해 잠시 생각해보십시오. 아마'Coefficients [, 2] <- c (1)'을 할 것입니다. – Roland

+0

@Roland 여전히 "알려지지 않은"상태입니다. – Ben

+0

"Coefficients [, 2] <- NA"를 추가하면 적어도 부울이며 알 수 없습니다. 그것은 도움이되지 않지만. – Ben

답변

1

귀하의 질문은 중복 그러나,이 질문에 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)) 
+0

고맙지 만 X를 수동으로 만들지 않으므로 적용 할 수 없으므로 변환시 오류가 발생합니다. 나는 그것을 질문에 더한다. – Ben

+0

예, 필자의 예는 추상적 인 예이며 "X"를 변환하려는 열의 이름 (예 : "Coefficients")으로 바꾸기 만하면됩니다. –

+0

질문에서 볼 수 있듯이 (업데이트) – Ben

관련 문제