Skip to content

I had 1 problem with r

3 messages · 정수빈, Jim Lemon, Peter Dalgaard

#
Hi My name is SuBin-Jung from Korea.
 
I can't write english well. so, understand me please.
 
I used R Gui(64-bit) program.
 
And in program, I imported this csv file named "emp.csv"
 
emp.csv
---------------------------------------------
empno,ename,job,mgr,hiredate,sal,comm,deptno
7369,SMITH,CLERK,7902,1980-12-17,800,,20
7499,ALLEN,SALESMAN,7698,1981-02-20,1600,300,30
7521,WARD,SALESMAN,7698,1981-02-03,1250,500,30
7566,JONES,MANAGER,7839,1981-03-02,2975,,20
7654,MARTIN,SALESMAN,7698,1981-10-22,1250,1400,30
7698,BLAKE,MANAGER,7839,1981-05-01,2850,,30
7782,CLARK,MANAGER,7839,1981-09-06,2450,,10
7788,SCOTT,ANALYST,7566,1982-12-08,3000,,20
7839,KING,PRESIDENT,,1981-11-17,5000,,10
7844,TURNER,SALESMAN,7698,1984-10-08,1500,,30
7876,ADAMS,CLERK,7788,1983-01-12,1100,,20
7900,JAMES,CLERK,7698,1981-12-03,950,,30
7902,FORD,ANALYST,7566,1981-12-13,3000,,20
7934,MILLER,CLERK,7782,1982-01-25,1300,,10
----------------------------------------------
 
Then I used command "read.csv"  ==> read.csv("emp.csv")
 
And I tried to find emp$ename with first alphabet 'A'.
 
So I inserted this command on program :
 
> subset(emp, ename==grep("^A+",ename,value=TRUE), select=c("ename","sal")) 
 
But the result is not found.
 
After that I tried to other alphabets( B,S,W....)
 
> subset(emp, ename==grep("^S+",ename,value=TRUE), select=c("ename","sal"))
 
Then I can find two data.
 
  ename  sal
1 SMITH  800
8 SCOTT 3000


Why can't I find emp$ename with first alphavet 'A'??
I think this is error. 
Please give me answer, sir.
#
Hi SuBin,
This seems to work:

emp<-read.table(text="empno,ename,job,mgr,hiredate,sal,comm,deptno
 7369,SMITH,CLERK,7902,1980-12-17,800,,20
 7499,ALLEN,SALESMAN,7698,1981-02-20,1600,300,30
 7521,WARD,SALESMAN,7698,1981-02-03,1250,500,30
 7566,JONES,MANAGER,7839,1981-03-02,2975,,20
 7654,MARTIN,SALESMAN,7698,1981-10-22,1250,1400,30
 7698,BLAKE,MANAGER,7839,1981-05-01,2850,,30
 7782,CLARK,MANAGER,7839,1981-09-06,2450,,10
 7788,SCOTT,ANALYST,7566,1982-12-08,3000,,20
 7839,KING,PRESIDENT,,1981-11-17,5000,,10
 7844,TURNER,SALESMAN,7698,1984-10-08,1500,,30
 7876,ADAMS,CLERK,7788,1983-01-12,1100,,20
 7900,JAMES,CLERK,7698,1981-12-03,950,,30
 7902,FORD,ANALYST,7566,1981-12-13,3000,,20
 7934,MILLER,CLERK,7782,1982-01-25,1300,,10",
 header=TRUE,sep=",")

emp[grep("^A",emp$ename),c("ename","sal")]
  ename  sal
2  ALLEN 1600
11 ADAMS 1100

Jim
On Mon, Aug 22, 2016 at 3:06 PM, ??? <subincj at naver.com> wrote:
#
On 22 Aug 2016, at 07:06 , ??? <subincj at naver.com> wrote:

            
Please do not post in HTML (reason should be pretty obvious).

Your logic is wrong: 

ename==grep("^A+",ename,value=TRUE)

Right hand side evaluates to c("ALLAN","ADAMS"); you then compare this to ename, which by recycling means that you compare 

SMITH to ALLAN, 
ALLAN to ADAMS, 
WARD to ALLAN, 
JONES to ADAMS,
...
TURNER to ADAMS,
ADAMS to ALLAN
...


See?

subset(emp, grepl("^A+",ename), select=c("ename","sal")) 

should work (and is simpler, too)

-pd