Reputation: 1957
f <- readLines('/home/dogus/Desktop/phonezzzz/vcards.txt', encoding="UTF-8")
#empty lists
names <- c()
numbers <- c()
phonezz <- function(){
for(i in 1:length(f)){
if("FN" == substring(f[i],0,2)){
name <- substring(f[i],4) # doesn't run
number <- substring(f[i+1],10) # doesn't run
names <- c(names, name) # doesn't run ; append 'name' into the 'names' list
numbers <- c(numbers, number) # doesn't run
print("Hello World")}
else{
print("------")
next
}
}
}
phonezz()
names
Here is the gist link: https://gist.github.com/dgsbicak/b9e2d4fb1dbb70c964295ef88120f404
I use R Studio.
When I run this script, in Global Environment section it shows:
Values:
f chr[1:3000] "some string" "some string2" "FN:213123123123" ...
names NULL (empty)
numbers NULL (empty)
Functions:
phonezz function ()
So name and number variables are not even exists; names list shows an empty list. And as I said it returns lots of "Hello World". So I dont understand what is the problem?
Kind Regards,
EDIT:
f <- readLines('/home/dogus/Desktop/phonezzzz/vcards.txt', encoding="UTF-8")
names <- c()
numbers <- c()
phonezz <- function() {for(i in 1:length(f))
{
if("FN" == substring(f[i],0,2)){
name <<- substring(f[i],4)
number <<- substring(f[i+1],10)
names <<- c(names, name)
numbers <<- c(numbers, number)
print("Hello World")
return(list(names, numbers))
}
else{
print("------")
next
}
}
}
phone <- phonezz()
phone
Now the result is:
Values:
f chr[1:3089] "asdasd" "asdasd" ...
name "just last person's name"
number "just last person's number"
names "just last person's name in the list"
numbers "just last person's number in the list"
phone List of 2
Functions:
phonezz function ()
Upvotes: 0
Views: 53
Reputation: 263411
This does not create a list:
numbers <- c()
Proof:
is.list(c())
[1] FALSE
Use instead:
my.names <- list() #don't use `names` ... it's the name of a function
numbers <- list()
Then another problem is that a for
-loop does not return anything (except, it does return NULL, but that's rarely useful). It only operates in the local environment of your function. A further (minor) problem is that indexing in R starts at 1 (NOT 0) so substring(f[i],0,2)
probably doesn't really make sense. If you had commented your code, we might know if you wanted 2 or 3 letters. I'm guessing 2, since you compare to "FN". Use substring(f[i],1,2)
. And then look at:
substring("string", 4)
[1] "ing" # so it returns the entire "tail" of the the input character value
Then there is a further problem that your for
-loop is overwriting EVERY value. So you should not be surprised if you only get one set of values. You need to index items inside the for
-loop and then OUTSIDE the loop you need to additionally apply the return
operation. The <<-
is only going to push those values one level "up" which is still inside the function environment. Nothing is going to happen outside.
If you had a complete example we could deliver a worked solution but I think this should help you understand the several problems that are immediately visible in your effort to start learning R. See [MCVE] and "how to make a great reproducible example in R"
Upvotes: 1
Reputation: 339
The variables names
and numbers
do not exist outside of the function phonezz()
Add the return(list(names, numbers)
as suggested above.
Then call the function as test_list <- phonezz()
. The variable test_list
should contain the names and numbers.
Upvotes: 1