> uc<-unique(r1$COMPANY)
> uc
[1] AZTEC CALIBER POINT COGNIZANT CYBAGE CYBAGE DLF
[7] GODREJ AND BOYCE LTD. HCL TECHNOLOGIES I-FLEX INFOCEPTS INFOSYS JATAAYU SOFTWARE (P) LTD.
[13] KANBAY KPIT L & T LTD. L & T INFOTECH MASTEK mBlazon SOLUTION PVT. LTD.
[19] MOTOR INDUSTRIES LTD NOVATECH PATNI COMPUTER SOFTWARE RF ARRAYS S.M. WIRELESS PVT. LTD. SATYAM COMPUTERS
[25] SATYAM COMPUTERS SATYAM COMPUTERS LTD. SHOBHA DEVELOPERS SOHAM's FOUNDTION ENGG. SYNTEL LTD. TCS
[31] TECH MAHINDRA LTD. ULTRA TECH CEMENT VRITTI SOLUTIONS ABO SOFTWARE ARTEFACT PROJECTS LTD. EATON
[37] FORCE MOTORS H.C.C. HEXAWARE TECHNOLOGIES HJB GROUP COMPANY, OMAN IBM DAKSH INDIAN MILITARY ACADEMY
[43] INDO RAMA SYNTHETICS INFOSPECTRUM PVT. LTD. JYOTI STRUCTUIRES LTD. KALPATARU KONE ELEVATORS L & T ( e- SOLUTIONS)
[49] LAMBENT MAHINDRA & MAHINDRA LTD. MAYTAS INFRA PVT. LTD. MOTOR INDUSTRIES LTD . ORIENT CEMENT PERSISTENT SYSTEMS PVT.LTD
[55] PREMIERE TECHNMOLOGY SCHNEIDER SETH CONSTRUCTION SIEMENS SIMPLEX SMS PARYAWARAN
[61] SOFT LINK INTERNATIONAL VARROC ENGINEERING, PUNE
> f<-length(uc)
> tt<-mat.or.vec(length(uc),3)
> for(n in 1:f)
+ {
+ tt[n,1]=uc[n]
+ tt[n,2]=5
+ tt[n,3]=9
+ }
> tt
[,1] [,2] [,3]
[1,] 3 5 9
[2,] 4 5 9
[3,] 5 5 9
[4,] 6 5 9
[5,] 7 5 9
[6,] 8 5 9
[7,] 11 5 9
[8,] 13 5 9
[9,] 16 5 9
[10,] 20 5 9
[11,] 22 5 9
[12,] 23 5 9
[13,] 26 5 9
[14,] 28 5 9
[15,] 29 5 9
[16,] 31 5 9
[17,] 34 5 9
[18,] 36 5 9
[19,] 37 5 9
[20,] 39 5 9
[21,] 41 5 9
[22,] 44 5 9
[23,] 45 5 9
[24,] 46 5 9
[25,] 47 5 9
[26,] 48 5 9
[27,] 51 5 9
[28,] 56 5 9
[29,] 57 5 9
[30,] 58 5 9
[31,] 59 5 9
[32,] 60 5 9
[33,] 62 5 9
[34,] 1 5 9
[35,] 2 5 9
[36,] 9 5 9
[37,] 10 5 9
[38,] 12 5 9
[39,] 14 5 9
[40,] 15 5 9
[41,] 17 5 9
[42,] 18 5 9
[43,] 19 5 9
[44,] 21 5 9
[45,] 24 5 9
[46,] 25 5 9
[47,] 27 5 9
[48,] 30 5 9
[49,] 32 5 9
[50,] 33 5 9
[51,] 35 5 9
[52,] 38 5 9
[53,] 40 5 9
[54,] 42 5 9
[55,] 43 5 9
[56,] 49 5 9
[57,] 50 5 9
[58,] 52 5 9
[59,] 53 5 9
[60,] 54 5 9
[61,] 55 5 9
[62,] 61 5 9
> strsplit(r1$COMPANY," ")
Error in strsplit(r1$COMPANY, " ") : non-character argument
我希望t [n,1]存储向量uc中存在的r1 $ COMPANY的所有唯一值,但它显示一些随机数。请帮助解决错误。另外我想知道如何获取第一个单词来自r1 $ COMPANY。我试图将它拆分为空格字符,但它显示错误。
答案 0 :(得分:0)
# I think `uc` is factor in your case (check using str(uc)). If it is character, it will give the solution as expected. Consider the following example:
uc<-names(mtcars)
#str(uc)
#chr [1:11] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
f<-length(uc)
tt<-mat.or.vec(f,3)
for(n in 1:f) {
tt[n,1]<-uc[n]
tt[n,2]<-5
tt[n,3]<-9
}
> tt
tt
[,1] [,2] [,3]
[1,] "mpg" "5" "9"
[2,] "cyl" "5" "9"
[3,] "disp" "5" "9"
[4,] "hp" "5" "9"
[5,] "drat" "5" "9"
[6,] "wt" "5" "9"
[7,] "qsec" "5" "9"
[8,] "vs" "5" "9"
[9,] "am" "5" "9"
[10,] "gear" "5" "9"
[11,] "carb" "5" "9"
答案 1 :(得分:0)
正如@Jason和@Metrics所提到的,uc
似乎是一个因素而不是一个角色。要解决此问题,请按以下方式定义uc
:
uc <- with(r1, as.character(unique(COMPANY)))
这里的关键是as.character()
,它将一个因子变量转换为一个字符变量。任何字符串操作函数现在都应该在uc
上工作。