英文:
What are those red lines with dots in RStudio editor
问题
当使用haven
的read_sav
从SPSS导入数据时,我得到一些values
,如"1. Tout à fait d?accord"
或"2. Plutôt d?accord"
。
这些数值似乎在句点和文本之间有空格,然而,它们实际上不是"正常"的空格,因为无法使用它们引用它们(即%>% filter(values == "1. Tout à fait d?accord")
将不起作用。
一个朋友告诉我,在STATA中打开数据,然后将这些行复制到RStudio编辑器中,会产生这些奇怪的红色字符(如图所示),现在代码可以运行。如何在不经过STATA的情况下在R中适当读取这些数据?
英文:
When importing data from SPSS with haven
's read_sav
I get some values
like "1.     Tout à fait d?accord"
or "2.     Plutôtd?accord"
.
Those values seem to have whitespaces between the period and the text, however, they are not actually "normal" whitespaces, as they can't be referenced using them (i.e. %>% filter(values == "1. Tout à fait d?accord")
) won't work.
A friend told me that opening the data in STATA and then copying the lines to RStudio editor produced these weird red characters (shown in image), and the code now works. How can I read these data in R adequately without going through STATA?
答案1
得分: 1
Are you sure that these have 4 white spaces? It is quite likely that the data has tab
as spaces which is different.
When you write into a text editor:
"I'm tab separated"
Then copy it to the console to assign it in R:
TestString <- "I'm tab separated"
To test whether it is the same as 4 white spaces we can run the following test.
# With 4 white spaces
TestString == "I'm tab separated"
[1] FALSE
# Tab separated
TestString == "I'm\ttab\tseparated"
[1] TRUE
By knowing this you can either replace the tab in the file or just address it during the subsetting (which is probably better).
英文:
Are you sure that these have 4 white spaces? It is quite likely that the data has tab
as spaces which is different.
When you write into a text editor:
"I'm tab separated"
Then copy it to the console to assign it in R:
TestString <- "I'm tab separated"
To test whether it is the same as 4 white spaces we can run the following test.
# With 4 white spaces
TestString == "I'm tab separated"
[1] FALSE
# Tab separated
TestString == "I'm\ttab\tseparated"
[1] TRUE
By knowing this you can either replace the tab in the file or just address it during the subsetting (which is probably better).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论