2017-11-21 9 views
2

데이터 프레임의 변수에서 정보를 추출하려고합니다. 나는 R 3.3.3을 사용하고있다. 로 포맷문자열에서 연속 된 콜론으로 문자를 추출합니다.

정보는 다음과 같습니다

t <- "China: Officially the People's Republic of China (PRC), is a unitary sovereign state in East Asia and the world's most populous country, with a population of around 1.404 billion. GUAM: Is an unincorporated and organized territory of the United States in Micronesia in the western Pacific Ocean. MICRONESIA: Is a subregion of Oceania, comprising thousands of small islands in the western Pacific Ocean. It has a shared cultural history with two other island regions, Polynesia to the east and Melanesia to the south. DOMINCAN REPUBLIC: Is a country located on the island of Hispaniola, in the Greater Antilles archipelago of the Caribbean region." 

내가 지금처럼 별도의 변수로 각 섹션을 분해하고 싶습니다 :

w = "China: Officially the People's Republic of China (PRC), is a unitary sovereign state in East Asia and the world's most populous country, with a population of around 1.404 billion." 

x = "GUAM: Is an unincorporated and organized territory of the United States in Micronesia in the western Pacific Ocean." 

y = "MICRONESIA: Is a subregion of Oceania, comprising thousands of small islands in the western Pacific Ocean. It has a shared cultural history with two other island regions, Polynesia to the east and Melanesia to the south." 

z = "DOMINCAN REPUBLIC: Is a country located in the island of Hispaniola, in the Greater Antilles archipelago of the Caribbean region." 

나는이 정보를 추출하려고 몇 가지 어려움을 겪고 있어요. thisthis과 같은 질문은 매우 도움이되었습니다. 이것들로부터, 나는 어떤 형태의 stringr/gsub가이 정보를 얻기 위해 사용될 수 있다고했지만, gsub 문 내에서 범위를 지정하는 방법을 알 수는 없다.

내가 어떻게 제 1 부분 끌어 운동 할 수 있었다 :

>test4 <- gsub("(.*{1})(:.*)","\\1", t) 

[1] "CHINA: Officially the People's Republic of China (PRC), is a unitary sovereign state in East Asia and the world's most populous country, with a population of around 1.404 billion. GUAM: Is an unincorporated and organized territory of the United States in Micronesia in the western Pacific Ocean. MICRONESIA: Is a subregion of Oceania, comprising thousands of small islands in the western Pacific Ocean. It has a shared cultural history with two other island regions, Polynesia to the east and Melanesia to the south. DOMINCAN REPUBLIC" 

내 전반적인 질문을 준다 :

[1] "CHINA: Officially the People's Republic of China (PRC), is a unitary sovereign state in East Asia and the world's most populous country, with a population of around 1.404 billion. GUAM: Is an unincorporated and organized territory of the United States in Micronesia in the western Pacific Ocean. MICRONESIA: Is a subregion of Oceania, comprising thousands of small islands in the western Pacific Ocean. It has a shared cultural history with two other island regions, Polynesia to the east and Melanesia to the south. DOMINCAN REPUBLIC" 

이 될 것이다 만약 문자열의 끝에서 "DOMINICAN REPUBLIC"부분을 정리할 필요가 없다면 좋을 것입니다. 요약

:

당신은 콜론의 연속에 의해 문자열에서 문자를 추출하는 방법

1.? (1 ~ 2 콜론, 2 ~ 3 등)

2. 콜론의 앞부분에도 단어를 유지할 수있는 방법이 있습니까?

모든 정보 또는 지침을 주시면 감사하겠습니다.

답변

2

당신은 적절한 정규식으로 strsplit를 사용할 수 있습니다

strsplit(t, "\\.\\s(?=[\\w\\s]+:)", perl=TRUE) 

또는

stringr::str_split(t, "\\.\\s(?=[\\w\\s]+:)") 

참고 :

  1. \\.\\s 리터럴 점과 공간을 일치합니다.
  2. (?=[\\w\\s]+:)은 콜론 다음에 단어 문자 또는 공백을 하나 이상 일치시키는 긍정적 인 미리보기입니다.
  3. \\.\\s(?=[\\w\\s]+:)은 단어 문자 또는 공백이 하나 이상 있고 콜론이 바로 뒤에 오는 경우에만 점 및 공백과 일치합니다. 이것은 각 단락의 끝이 될 것입니다.
  4. strsplit 내에서 정규식을 사용하고 있기 때문에 정규식과 일치하는 부분으로 나눌 수 있습니다. 결과적으로 각 단락의 끝 부분이 분할됩니다. 미리보기/숨기기를 사용하려면
  5. perl=TRUE이 필요합니다.

결과 :

[[1]] 
[1] "China: Officially the People's Republic of China (PRC), is a unitary sovereign state in East Asia and the world's most populous country, with a population of around 1.404 billion"           
[2] "GUAM: Is an unincorporated and organized territory of the United States in Micronesia in the western Pacific Ocean"                           
[3] "MICRONESIA: Is a subregion of Oceania, comprising thousands of small islands in the western Pacific Ocean. It has a shared cultural history with two other island regions, Polynesia to the east and Melanesia to the south" 
[4] "DOMINCAN REPUBLIC: Is a country located on the island of Hispaniola, in the Greater Antilles archipelago of the Caribbean region." 
+0

위대한 작품! 데이터를 어떻게 분할하고 있는지 완전히 이해하는 데 시간이 걸릴 것입니다.하지만 대단히 감사합니다! – Jbnimble

+0

대단히 감사합니다! 정말 고마워! – Jbnimble

1

다음을 기준으로 R은 어떻게됩니까?

# Your sample string 
t <- "China: Officially the People's Republic of China (PRC), is a unitary sovereign state in East Asia and the world's most populous country, with a population of around 1.404 billion. GUAM: Is an unincorporated and organized territory of the United States in Micronesia in the western Pacific Ocean. MICRONESIA: Is a subregion of Oceania, comprising thousands of small islands in the western Pacific Ocean. It has a shared cultural history with two other island regions, Polynesia to the east and Melanesia to the south. DOMINCAN REPUBLIC: Is a country located on the island of Hispaniola, in the Greater Antilles archipelago of the Caribbean region."; 

# Get position of regexp matches 
matches <- data.frame(
    idx = unlist(gregexpr(pattern = "([A-Z]*\\s*[A-Z]+:|\\w+:)", t)), 
    len = c(diff(unlist(gregexpr(pattern = "([A-Z]*\\s*[A-Z]+:|\\w+:)", t))), nchar(t)) 
); 

# Get substrings based on positions and store in list 
lst <- apply(matches, 1, function(x) { 
    trimws(substr(t, x[1], sum(x) - 1)); 
}) 
lst; 

#[1] "China: Officially the People's Republic of China (PRC), is a unitary sovereign state in East Asia and the world's most populous country, with a population of around 1.404 billion." 
#[2] "GUAM: Is an unincorporated and organized territory of the United States in Micronesia in the western Pacific Ocean." 
#[3] "MICRONESIA: Is a subregion of Oceania, comprising thousands of small islands in the western Pacific Ocean. It has a shared cultural history with two other island regions, Polynesia to the east and Melanesia to the south. DOMINCAN" 
#[4] "DOMINCAN REPUBLIC: Is a country located on the island of Hispaniola, in the Greater Antilles archipelago of the Caribbean region." 

참고 : 예는 모두 대문자로 여러 단어 국가 (DOMINCAN REPUBLIC), 모두 대문자 한 단어로 국가 (예 : GUAM), 및 "첫 문자 대문자"를 포함하고 있기 때문에 정규 표현식 매칭 국가 조금 어색 국가 (China).

+0

당신을 감사합니다! 나는 이것을 밖으로 시도하고 그것은 위대한 일했습니다! – Jbnimble